一、参考资料
两两交换链表中的节点
题目链接/文章讲解/视频讲解: https://programmercarl.com/0024.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
删除链表的倒数第N个节点
面试题 02.07. 链表相交
环形链表II
题目链接/文章讲解/视频讲解:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html
二、LeetCode24. 两两交换链表中的节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
// 这个题如果能真正理解好逻辑就清楚啦
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
// 先创建一个虚拟节点
ListNode *dummyHead = new ListNode(0);
// 虚拟节点指向头结点,保证头节点的操作与其他节点一样
dummyHead->next = head;
ListNode *cur = dummyHead;
// 因为要调换两个节点的内容,因此while条件的判断需要多加注意
while (cur->next != NULL && cur->next->next != NULL) {
// 记录临时节点
ListNode *tmp1 = cur->next;
// 这个表示第三个节点
ListNode *tmp2 = cur->next->next->next;
cur->next = cur->next->next; // 第一步
cur->next->next = tmp1; // 第二步
tmp1->next = tmp2; // 第三步
// 移动两位,准备下一轮节点交换
cur = cur->next->next;
}
return dummyHead->next;
}
};
三、LeetCode19-删除链表的倒数第N个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
// 这题用快慢指针,快指针先走n步,然后慢指针从链表首节点开始,
// 此后,快慢指针一起走,直到快指针走到队列尾
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode *slow = dummyHead;
ListNode *fast = dummyHead;
while (fast != NULL && n--) {
fast = fast->next;
// n--;
}
// fast再提前走一步,因为需要让slow指向删除节点的上一个节点
fast = fast->next;
// 快慢指针一起走
while (fast != NULL) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummyHead->next;
}
};
四、LeetCode面试题 02.07. 链表相交
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// 交点不是数值相等,而是指针相等
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;
}
};
五、LeetCode142-环形链表II
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
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;
// 快慢指针相遇
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2; // 返回环的入口
}
}
return NULL;
}
};
Day04总结:
今天因有事出门啦,比较草率的打了卡。这一天的后俩题还需要再看看,尤其是环形链表。