首页 > 编程语言 >代码随想录算法训练营第四天|24. 两两交换链表中的节点,19.删除链表的倒数第N个节点,面试题 02.07. 链表相交,142.环形链表II

代码随想录算法训练营第四天|24. 两两交换链表中的节点,19.删除链表的倒数第N个节点,面试题 02.07. 链表相交,142.环形链表II

时间:2023-01-14 02:33:05浏览次数:65  
标签:ListNode 随想录 fast next 链表 curA 节点

一、参考资料

两两交换链表中的节点

题目链接/文章讲解/视频讲解: https://programmercarl.com/0024.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html

删除链表的倒数第N个节点

题目链接/文章讲解/视频讲解:https://programmercarl.com/0019.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.html

面试题 02.07. 链表相交

题目链接/文章讲解:https://programmercarl.com/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.html

环形链表II

题目链接/文章讲解/视频讲解:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html

二、LeetCode24. 两两交换链表中的节点

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. // 这个题如果能真正理解好逻辑就清楚啦
  12. class Solution {
  13. public:
  14. ListNode* swapPairs(ListNode* head) {
  15. // 先创建一个虚拟节点
  16. ListNode *dummyHead = new ListNode(0);
  17. // 虚拟节点指向头结点,保证头节点的操作与其他节点一样
  18. dummyHead->next = head;
  19. ListNode *cur = dummyHead;
  20. // 因为要调换两个节点的内容,因此while条件的判断需要多加注意
  21. while (cur->next != NULL && cur->next->next != NULL) {
  22. // 记录临时节点
  23. ListNode *tmp1 = cur->next;
  24. // 这个表示第三个节点
  25. ListNode *tmp2 = cur->next->next->next;
  26. cur->next = cur->next->next; // 第一步
  27. cur->next->next = tmp1; // 第二步
  28. tmp1->next = tmp2; // 第三步
  29. // 移动两位,准备下一轮节点交换
  30. cur = cur->next->next;
  31. }
  32. return dummyHead->next;
  33. }
  34. };

三、LeetCode19-删除链表的倒数第N个节点

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. // 这题用快慢指针,快指针先走n步,然后慢指针从链表首节点开始,
  12. // 此后,快慢指针一起走,直到快指针走到队列尾
  13. class Solution {
  14. public:
  15. ListNode* removeNthFromEnd(ListNode* head, int n) {
  16. ListNode *dummyHead = new ListNode(0);
  17. dummyHead->next = head;
  18. ListNode *slow = dummyHead;
  19. ListNode *fast = dummyHead;
  20. while (fast != NULL && n--) {
  21. fast = fast->next;
  22. // n--;
  23. }
  24. // fast再提前走一步,因为需要让slow指向删除节点的上一个节点
  25. fast = fast->next;
  26. // 快慢指针一起走
  27. while (fast != NULL) {
  28. fast = fast->next;
  29. slow = slow->next;
  30. }
  31. slow->next = slow->next->next;
  32. return dummyHead->next;
  33. }
  34. };

四、LeetCode面试题 02.07. 链表相交

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. // 交点不是数值相等,而是指针相等
  10. class Solution {
  11. public:
  12. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  13. ListNode* curA = headA;
  14. ListNode* curB = headB;
  15. int lenA = 0, lenB = 0;
  16. while (curA != NULL) { // 求链表A的长度
  17. lenA++;
  18. curA = curA->next;
  19. }
  20. while (curB != NULL) { // 求链表B的长度
  21. lenB++;
  22. curB = curB->next;
  23. }
  24. curA = headA;
  25. curB = headB;
  26. // 让curA为最长链表的头,lenA为其长度
  27. if (lenB > lenA) {
  28. swap (lenA, lenB);
  29. swap (curA, curB);
  30. }
  31. // 求长度差
  32. int gap = lenA - lenB;
  33. // 让curA和curB在同一起点上(末尾位置对齐)
  34. while (gap--) {
  35. curA = curA->next;
  36. }
  37. // 遍历curA 和 curB,遇到相同则直接返回
  38. while (curA != NULL) {
  39. if (curA == curB) {
  40. return curA;
  41. }
  42. curA = curA->next;
  43. curB = curB->next;
  44. }
  45. return NULL;
  46. }
  47. };

五、LeetCode142-环形链表II

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *detectCycle(ListNode *head) {
  12. ListNode* fast = head;
  13. ListNode* slow = head;
  14. while(fast != NULL && fast->next != NULL) {
  15. slow = slow->next;
  16. fast = fast->next->next;
  17. // 快慢指针相遇
  18. if (slow == fast) {
  19. ListNode* index1 = fast;
  20. ListNode* index2 = head;
  21. while (index1 != index2) {
  22. index1 = index1->next;
  23. index2 = index2->next;
  24. }
  25. return index2; // 返回环的入口
  26. }
  27. }
  28. return NULL;
  29. }
  30. };

Day04总结:

今天因有事出门啦,比较草率的打了卡。这一天的后俩题还需要再看看,尤其是环形链表。

标签:ListNode,随想录,fast,next,链表,curA,节点
From: https://www.cnblogs.com/ucaszym/p/17051097.html

相关文章