感觉链表中主要的就是灵活使用指针,能想起来的指针:
- 快慢指针
- 前后指针
- 相邻指针
- 等间隔指针
24. 两两交换链表中的节点
自己的版本
需要注意的事项:
- 使用dummyhead,方便进行操作
- 使用两个相邻的指针cur和next,这样方便两两交换
- 我认为最关键的:
- 防止next指针访问越界,每次循环完之后移动指针时,只移动cur
- 每次判断cur和cur->next,判断是否为nullptr,这样最后剩下的单个节点不会交换
- 每次while循环开始表示cur->next是完整节点,这时候再更新next
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *dummyhead = new ListNode(-1, head);
ListNode *pre = dummyhead;
ListNode *cur = head;
ListNode *next = nullptr;
//防止next访问到空,每次首先判断cur和cur->next,在移动next
while (cur != nullptr && cur->next != nullptr) {
//下面的情况下,cur和next一定都存在
next = cur->next;
cur->next = next->next;
pre->next = next;
next->next = cur;
//换成下一次循环的cur和pre
pre = cur;
cur = cur->next;
}
return dummyhead->next;
}
};
## 代码随想录
和我思路一样
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
ListNode* cur = dummyHead;
while(cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp = cur->next; // 记录临时节点
ListNode* tmp1 = cur->next->next->next; // 记录临时节点
cur->next = cur->next->next; // 步骤一
cur->next->next = tmp; // 步骤二
cur->next->next->next = tmp1; // 步骤三
cur = cur->next->next; // cur移动两位,准备下一轮交换
}
return dummyHead->next;
}
};
19.删除链表的倒数第N个节点
自己的版本
关键的点:
- 寻找倒数第n个指针,那么我使用两个间隔n+1的指针
- 当rear指针指向nullptr,pre指针则指向要删除的节点的前一个,直接操作
- 注意一下这两个指针具体差多少吧,不行可以画一下
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *dummyhead = new ListNode(-1, head);
ListNode *pre = dummyhead;
ListNode *rear = dummyhead;
n += 1;
while (n--) {
rear = rear->next;
}
while (rear != nullptr) {
pre = pre->next;
rear = rear->next;
}
ListNode *temp = pre->next;
pre->next = pre->next->next;
delete temp;
return dummyhead->next;
}
};
代码随想录
-
和我想得一样,叫做快慢指针
-
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* dummyHead = new ListNode(0); dummyHead->next = head; ListNode* slow = dummyHead; ListNode* fast = dummyHead; while(n-- && fast != NULL) { fast = fast->next; } fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点 while (fast != NULL) { fast = fast->next; slow = slow->next; } slow->next = slow->next->next; //C++释放内存的逻辑 ListNode *tmp = slow->next; slow->next = tmp->next; delete nth; return dummyHead->next; } };
面试题 02.07. 链表相交
自己的版本
直接用一个哈希表存第一个链表节点地址,再去遍历第二个。如果有重复,说明找到交点了,直接返回
但是哈希表占用空间多了不少 O(M+N)
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_set<ListNode *> set;
ListNode *ptr1 = headA;
while (ptr1 != nullptr) {
set.emplace(ptr1);
ptr1 = ptr1->next;
}
ListNode *ptr2 = headB;
while (ptr2 != nullptr) {
if (set.find(ptr2) != set.end()) {
return ptr2;
}
ptr2 = ptr2->next;
}
return nullptr;
}
};
代码随想录
既然是相交节点,相交节点的后续部分一定完全一致,毕竟相交节点对于两个列表来说就是同一个节点,那么也就是说相交节点以及后续部分一定是位于列表最后部分的。
只需要把长列表的起点使得该起点到结尾的距离与短列表开头到结尾开头的距离一致,之后同步向后移,只要有相交节点那么必然能同时遇到。
好吧,看来用哈希表有点取巧
链表尾部对其,使用两个指针同时向后移动O(1)
关键点:
- 使用两个链表的长度,方便对齐尾部
- 然后将两个指针指向对其的位置开始向后移动找到地址相同的返回
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != NULL) { // 求链表A的长度
lenA++;
curA = curA->next;
}
while (curB != NULL) { // 求链表B的长度
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
swap (lenA, lenB);
swap (curA, curB);
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap--) {
curA = curA->next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != NULL) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
142.环形链表II
自己的版本
哈希表,空间复杂O(M+N),不提了
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_set<ListNode *> set;
ListNode *ptr = head;
while (ptr != nullptr) {
if (set.find(ptr) != set.end()) {
return ptr;
}
set.emplace(ptr);
ptr = ptr->next;
}
return nullptr;
}
};
代码随想录
使用一个快慢指针加上一些数学计算
具体推导过程看网站,最后可以得到一个公式x = (n - 1) (y + z) + z
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点
也就是在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。
让index1和index2同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点。
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2; // 返回环的入口
}
}
return NULL;
}
};