Leetcode 24-两两交换链表中的节点
题目描述:
https://leetcode.cn/problems/swap-nodes-in-pairs/description/
解题思路
设置虚拟头节点确保在操作链表时头节点和其他节点的一致性,由上图可知,当链表中有偶数个节点时,操作循环的结束条件是 cur->nextNULL,当链表中有奇数个节点时,循环结束条件为 cur->next->nextNULL。具体代码如下:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyhead = new ListNode(0);
ListNode* cur = dummyhead;
dummyhead->next = head;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* temp1 = cur->next;//保存节点1因为一旦声明cur->next = cur->next->next后无法找到与节点1的连接
cur->next = cur->next->next;
ListNode* temp2 = cur->next->next;//保存节点3因为一旦声明cur->next(节点2)->next = temp1会让2与3的连接消失,无法再找到节点3
cur->next->next = temp1;
cur->next->next->next = temp2;
cur = cur->next->next;
}
ListNode* result = dummyhead->next;
delete dummyhead;
return result;
}
};
Leetcode 19-删除链表的倒数第 N 个节点
题目描述:
https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
解题思路
这里使用双指针的解题方法,快指针比慢指针先走 n+1 步(n+1 而非 n 的原因是删除倒数第 n 个元素需要找到第 n+1 的元素,才能让倒数第 n+1 个元素的 next 指向它的 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;
while (n + 1 > 0 && fast != nullptr) {
fast = fast->next;
n--;
}
while (fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
ListNode* temp = slow->next;
slow->next = slow->next->next;
delete temp;
ListNode* result = dummyhead->next;
delete dummyhead;
return result;
}
};
链表相交
题目描述:
https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/
解题思路
两个链表的交点不是数值相同,而是指针相同。因此我们可以让 AB 两个链表末尾对齐,比较两个指针所指的位置是否相同判断是否存在交点。
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != nullptr) {
lenA++;
curA = curA->next;
}
while (curB != nullptr) {
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
//保证链表A为最长链表,其中curA为A链表的头
if (lenB > lenA) {
swap(lenA, lenB);
swap(curA, curB);
}
//求长度差
int gap = lenA - lenB;
while (gap)
{
curA = curA->next;
gap--;
}
while (curA != nullptr) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
Leetcode 142-环形链表 Ⅱ
题目描述:
https://leetcode.cn/problems/linked-list-cycle-ii/description/
解题思路
这道题有两个问题,一个是先判断链表中是否有环,第二个是需要判断环的入口在哪里。
对于第一个问题,可以通过双指针的思路进行判断,设置快慢两个指针,快指针每次移动两个步长,慢指针移动一个步长,如果链表中没有环,则快慢指针一定不会相遇,如果有环,则快慢指针一定会相遇(一定相遇的原因:快指针相对于慢指针每次走一个步长,不会出现跳过的情况)
对于第二个问题,要求解环的入口,可以通过下图列出表达式:
$2(x+y) = n(y+z)+x+y
$ //即慢节点行走的距离=快节点
由此可知:
x + y = n ( y + z ) x+y = n(y+z) x+y=n(y+z)
可得:
x = ( n − 1 ) ( y + z ) + z x=(n-1)(y+z)+z x=(n−1)(y+z)+z
令 n=1,可知:
x = z x=z x=z
class Solution {
public:
ListNode* detectCycle(ListNode* head) {
ListNode* fast = head;
ListNode* slow = head;
while (fast != nullptr && fast->next != nullptr) {
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;
}
};