代码随想录算法训练营第四天| 24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II
- 题目链接:[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/description/)
- 题目链接:[19.删除链表的倒数第N个节点](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)
- 题目链接:[面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)
- 题目链接:[142.环形链表II](https://leetcode.cn/problems/linked-list-cycle-ii/)
题目链接:24. 两两交换链表中的节点
思路
注意要将当前节点的下一个节点(cur->next)和当前节点下一个的下一个节点保存(cur->next->next)。
/**
* 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;
ListNode* temp;
ListNode* temp1;
while(cur->next != nullptr && cur->next->next != nullptr){
temp = cur->next;
temp1 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = temp;
temp->next = temp1;
cur = cur->next->next;
}
return dummyhead->next;
}
};
题目链接:19.删除链表的倒数第N个节点
思路
定义双指针,让快指针先走过n+1个节点,慢指针再移动,这样可以使得当快指针指向NULL时,慢指针刚好指向倒数第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) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
ListNode* fast = dummyhead;
ListNode* slow = dummyhead;
n++;
while(n-- && fast != nullptr){
fast = fast->next;
}
// fast = fast->next;
while(fast != nullptr){
fast = fast->next;
slow = slow->next;
}
ListNode* temp = slow->next;
slow->next = slow->next->next;
delete temp;
return dummyhead->next;
}
};
题目链接:面试题 02.07. 链表相交
思路
分别计算链表A和链表B的长度,保证让链表A的长度比链表B的长度长,计算两个链表的长度差,让链表A的curA指向链表B的头部,然后curA和curB再同时向后移动,找到相等的节点。
/**
* 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 *cur1 = headA;
ListNode *cur2 = headB;
int len1 = 0;
int len2 = 0;
while(cur1 !=NULL){
len1++;
cur1 = cur1->next;
}
while(cur2 !=NULL){
len2++;
cur2 = cur2->next;
}
cur1 = headA;
cur2 = headB;
if(len1<len2){
swap(len1,len2);
swap(cur1,cur2);
}
int gap = len1-len2;
while(gap--){
cur1 = cur1->next;
}
while(cur1 != NULL){
if(cur1 == cur2){
return cur1;
}
cur1 = cur1->next;
cur2 = cur2->next;
}
return NULL;
}
};
题目链接:142.环形链表II
思路
定义快慢指针,让快指针每次向后移动两步,慢指针每次移动一步,找到快慢指针相遇的节点,重新定义指针index1在相遇的位置(index1 = fast),定义指针index2在头节点,然后让index1和index2同时向后移动,直到相遇。
/**
* 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){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
ListNode *index1 = fast;
ListNode *index2 = head;
while(index1 != index2){
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}
};