两两交换链表节点
关键点
- 先搞个虚拟头节点链接上真正的头节点
- 用于遍历的cur指针从虚拟头节点开始
- 记得临时变量保存cur->next 和 cur->next->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;
while(cur->next != NULL && cur->next->next != NULL)
{
ListNode* temp = cur->next;
ListNode* temp1 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = temp;
cur->next->next->next = temp1;
cur = cur->next->next;
}
return dummyHead->next;
}
};
删除链表的倒数第 N 个结点
关键点
- 记得虚拟头节点链接真正的头节点
- 快慢指针先从虚拟头节点出发
- 快指针走n+1步,然后快慢指针同时移动直到快指针指向NULL,此时慢指针刚好指向要删除节点的前一个节点
- 正常的删除操作,记得释放内存。
我的代码
/**
* 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* far = dummyHead;
ListNode* slow = dummyHead;
int i=0;
while(i<n+1)
{
far = far->next;
i++;
}
while(far != NULL)
{
slow = slow->next;
far = far->next;
}
ListNode* temp = slow->next;
slow->next = slow->next->next;
delete temp;
temp = nullptr;
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;
// ListNode *tmp = slow->next; C++释放内存的逻辑
// slow->next = tmp->next;
// delete tmp;
return dummyHead->next;
}
};
环形链表
关键点
- 用快慢指针来寻找相遇点
- 从头节点和相遇点开始一步一步走到新的相遇点即为环形链表链接处
以上过程最好用草稿纸模拟手算一下
我的代码
/**
* 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* far = head;
ListNode* slow = head;
while(far!= NULL && far->next != NULL)
{
slow = slow->next;
far = far->next->next;
if(slow == far)
{
ListNode* index0 = head;
ListNode* index1 = far;
while(index0 != index1)
{
index0 = index0->next;
index1 = index1->next;
}
return index0;
}
}
return NULL;
}
};
卡哥的代码
/**
* 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;
// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2; // 返回环的入口
}
}
return NULL;
}
};
链表相交
关键点
- 要先算出链A和链B总的长度,再算出多出了gap多少
- 将长链的指针先移动gap的距离
- 此时两个指针同时移动看有无相同元素,有就是相交点
技巧 - 看题解说的把两条链表拉直了来看双指针就非常清晰了
- 用swap交换长的指针和链表长度
- 算完长度后指针记得指回头节点,第二轮从头开始
我的代码
/**
* 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;
int gap = 0;
while(curA != NULL)
{
curA = curA->next;
lenA++;
}
while(curB != NULL)
{
curB = curB->next;
lenB++;
}
curA = headA;
curB = headB;
if(lenB > lenA)
{
swap(lenA,lenB);
swap(curA, curB);
}
gap = lenA - lenB;
while(gap--)
{
curA = curA->next;
}
while(curA != NULL)
{
if(curA == curB)
{
return curA;
}
curA = curA->next;
curB = curB->next;
}
return 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;
}
};