【代码随想录二刷】Day4-链表-C++
admin
2024-05-23 12:38:36

代码随想录二刷Day4

今日任务

24.两两交换链表中的节点
19.删除链表的倒数第N个结点
面试题 02.07. 链表相交
142.环形链表II
语言:C++

24. 两两交换链表中的节点

链接:https://leetcode.cn/problems/swap-nodes-in-pairs/

/*** 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) {if(head == nullptr || head->next == nullptr) return head;ListNode* dummyHead = new ListNode(0, head);ListNode* pre = dummyHead;ListNode* cur = head;ListNode* next = head->next;while(next != nullptr){pre->next = next;ListNode* tmp = next->next;next->next = cur;cur->next = tmp;pre = cur;cur = cur->next;if(cur != nullptr) next = cur->next;else next = nullptr;}head = dummyHead->next;delete dummyHead;return head;}
};

19. 删除链表的倒数第N个结点

链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/

/*** 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, head);ListNode* slow = dummyHead, *fast = dummyHead;for(int i = 0; i < n; i++){fast = fast->next;}while(fast->next != nullptr){slow = slow->next;fast = fast->next;}ListNode* node = slow->next;slow->next = slow->next->next;delete node;head = dummyHead->next;delete dummyHead;return head;}
};

面试题 02.07. 链表相交

链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/

/*** 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) {if(headA == nullptr || headB == nullptr) return nullptr;ListNode* curA = headA;ListNode* curB = headB;while(curA != curB){if(curA == nullptr) curA = headB;else if(curB == nullptr) curB = headA;else{curA = curA->next;curB = curB->next;}}return curA;}
};

142. 环形链表II

链接:https://leetcode.cn/problems/linked-list-cycle-ii/
一些还没掌握太好的问题:
1.为什么快慢指针会在入环第一圈内就相遇?
2.如何确认环入口的位置,为什么可以这样确认?

/*** 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, *fast = head;while(fast != nullptr && fast->next != nullptr){slow = slow->next;fast = fast->next->next;if(slow == fast){slow = head;while(slow != fast){slow = slow->next;fast = fast->next;}return fast;}}return nullptr;}
};

相关内容

热门资讯

分享健康营养美食:简单制作方法 在快节奏的现代生活中,健康营养美食已成为我们追求平衡生活的重要一环。它不仅能满足味蕾,还能为身体注入...
厨房小白秒变大厨!这两道菜让全... 有没有过这种崩溃时刻?下班累成狗,打开冰箱只剩鸡翅和生菜,外卖又吃腻了,难道今晚要啃空气?别慌!今天...
【广医科普】小雪节气养生 小 雪 2025年11月22日 2025年11月22日我们迎来了冬季的第二个节气——小雪。此时天气...
原创 就... 今天来一道特别容易做,而且特别下饭的砂锅菜,全程没有加入一滴水,上桌还在兹拉兹拉地冒着热气,开盖就是...