24. 两两交换链表中的节点
这里采用虚拟头指针
/**
* 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* dummpHead = new ListNode(0); //设置一个虚拟头节点
dummpHead->next = head; //将虚拟头节点指向head,方便后续做删除操作
ListNode* cur = dummpHead;
while(cur->next != nullptr && cur->next->next != nullptr)
{
ListNode* tmp = cur->next; //记录临时节点
ListNode* tmp1 = cur->next->next->next;
cur->next = tmp->next; //步骤一
cur->next->next = tmp; //步骤二
cur->next->next->next = tmp1; //步骤三
cur = cur->next->next; //cur向后移动两位
}
return dummpHead->next; //返回虚拟指针的next
}
};
Leetcode 19 删除链表的倒数第N个节点
采用双指针
如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
/**
* 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;
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; //删除第n个节点
return dummyHead->next;
}
};
Leetcode 160 链表相交
我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,此时我们就可以比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到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* curA = headA;
ListNode* curB = headB;
int lenA = 0;
int lenB = 0;
while(curA != NULL)
{
lenA++;
curA = curA->next;
}
while(curB != NULL)
{
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenA < lenB)
{
swap(lenA,lenB);
swap(curA,curB);
}
int gap = lenA-lenB;
while(gap--){
curA = curA->next;
}
while(curA !=NULL)
{
if(curA == curB)
{
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
Leetcode 142 环形链表II
找到环形的节点
用快慢指针
fast走两步,slow走一步
判断是否有环:
fast指针一定先进入环中,如果fast指针和slow指针相遇的话,一定是在环中相遇,这是毋庸置疑的。
判断环节点的位置:
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
/**
* 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) { //保证快指针本身不为NULL并且下一个节点不为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;
}
};