链表
原地逆置
C++
class Solution {
public:
ListNode* reverseList(ListNode* head) {//链表无头节点 原地逆置
ListNode* pre=head;
ListNode* cur=NULL;
ListNode* t=NULL;//t=head->next 若head指向空链表会报错 非法访问其他空间
while(pre!=NULL){
t=pre->next;
pre->next=cur;
cur=pre;
pre=t;
}
head=cur;
return head;
}
};
快慢指针
LCR 140. 训练计划 II - 力扣(LeetCode)
让两个指针相差k个 只用遍历一轮
C++
class Solution {
public:
ListNode* trainingPlan(ListNode* head, int cnt) {
ListNode* f=head;//fast
ListNode* s=head;//slow
int count=0;
while(f!=NULL&&count<cnt){
count++;
f=f->next;
}
while(f!=NULL){
f=f->next;
s=s->next;
}
return s;
}
};
静态链表
用数组描述的链表
跳表
跳跃表、跳跃列表,在有序链表的基础上增加了"跳跃"的功能,有序链表实现而二分查找。
有点复杂 暂时不需要做