【代码随想录二刷】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;}
};

相关内容

热门资讯

奥地利小镇邂逅农民思想家多伊布... 旅游奥地利期间,每天与同行的徐静波教授晨起一同散步,有时沿着多瑙河,有时在维也纳的森林间,但给我印象...
读懂济南|从“一阵风”到“长红... 齐鲁晚报·齐鲁壹点 王贝艺大明湖畔的狂欢还未收尾,五龙潭的泼水狂欢又已开场;广智里的咖啡香还没散尽,...
黄河壶口瀑布水量大增 形成壮美...   9月9日,受降雨天气影响,位于晋陕峡谷的黄河壶口瀑布水量大增,主副瀑布连成一片,形成壮美瀑布群景...
“请3休13”点燃中秋国庆国内... 财联社9月10日讯(记者 胡皓琼)中秋前一日火车票开售,中秋国庆国内游预订提速。财联社记者从OTA平...
微言|游客暴增却带不来利润?“... 中秋、国庆假期将至,很多地方将迎来旅游旺季。然而,一些地方旅游人次创新高,民宿经营者却普遍反映利润摊...