24. 两两交换链表中的节点
题目链接/文章讲解/视频讲解:链接地址
代码思路:建立虚拟头节点,分析循环结束条件,如果链表是奇数最后一个元素不需要交换,则结束条件是cur->next->next != nullptr,如果链表是偶数则结束条件是 cur->next != nullptr。然后按照链表交换的原则,调整需要交换元素的两个指针的指向。
/**
* 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* dummy_head = new ListNode(0);
dummy_head->next = head;
ListNode* cur = dummy_head;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* temp1 = cur->next;
ListNode* temp2 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = temp1;
temp1->next = temp2;
cur = cur->next->next;
}
head = dummy_head->next;
delete dummy_head;
return head;
}
};
19.删除链表的倒数第N个节点
题目链接/文章讲解/视频讲解:链接地址
代码思路:利用快慢指针找到,倒数第n+1个节点(需要n前的的一个节点才能执行删除操作),快指针到达n+1个节点的时候,慢指针和快指针同时移动,直到快指针遇到空指针结束,此时慢指针的位置是倒数第n+1个节点,然后进行节点删除操作。
/**
* 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* dummpy_node = new ListNode(0);
dummpy_node->next = head;
ListNode* slow = dummpy_node;
ListNode* fast = dummpy_node;
n++;
while (n-- && fast != nullptr) {
fast = fast->next;
}
while (fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
ListNode* temp = slow->next;
slow->next = slow->next->next;
delete temp;
head = dummpy_node->next;
delete dummpy_node;
return head;
}
};
160.链表相交 (面试题 02.07. 链表相交)
题目链接/文章讲解:链接地址
代码思路:首先得到两个链表的长度,然后将短链表的尾部和长链表的尾部对其,再从长链表表和短链表头部对齐的位置开始遍历长链表,如果遍历过程有值相等就是我们要找的交点,没有则返回null.
/**
* 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 != nullptr) {
lenA++;
curA = curA->next;
}
while (curB != nullptr) {
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if (lenB > lenA) { // 保证链表A始终是长列表
swap (lenA, lenB);
swap (curA, curB);
}
int gap = lenA - lenB;
while (gap--) {
curA = curA->next;
}
while (curA != nullptr) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
142.环形链表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* slow = head;
ListNode* fast = head;
//fast->next也需要不为空,避免出现fast->null->next的情况
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
if (fast == slow) {
ListNode* index1 = head;
ListNode* index2 = fast;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}
};