当前位置:   article > 正文

代码随想录二刷Day2

代码随想录二刷Day2

203. 移除链表元素 - 力扣(LeetCode)

  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. class Solution {
  12. public:
  13. ListNode* removeElements(ListNode* head, int val)
  14. {
  15. //移除链表元素的核心就是找到需要删除元素的前一个节点
  16. //为了所有链表删除操作统一,可以采用虚拟头节点,dummyNode
  17. ListNode* dummyNode = new ListNode(0);
  18. dummyNode -> next = head;
  19. ListNode* cur = dummyNode;
  20. while(cur != nullptr && cur -> next != nullptr)
  21. {
  22. //删除一个节点之后不能直接向后移动,要停在原位防止漏删
  23. if(cur->next->val == val)
  24. {
  25. cur->next = cur->next->next;
  26. }
  27. else
  28. {
  29. cur = cur->next;
  30. }
  31. }
  32. return dummyNode->next;
  33. }
  34. };

707. 设计链表 - 力扣(LeetCode)

  1. class MyLinkedList {
  2. public:
  3. struct LinkedNode
  4. {
  5. int val;
  6. LinkedNode* next;
  7. LinkedNode(int val): val(val), next(nullptr){}
  8. };
  9. MyLinkedList() {
  10. dummyHead = new LinkedNode(0);
  11. size = 0;
  12. }
  13. int get(int index) {
  14. //无效下标
  15. if(index < 0 || index >= size)
  16. return -1;
  17. //有效下标
  18. LinkedNode* cur = dummyHead;
  19. //需要得到index指向的节点
  20. for(int i = 0; i <= index; i++)
  21. {
  22. cur = cur->next;
  23. }
  24. return cur->val;
  25. }
  26. void addAtHead(int val) {
  27. LinkedNode* cur = dummyHead;
  28. LinkedNode* newNode = new LinkedNode(val);
  29. newNode->next = dummyHead->next;
  30. dummyHead->next = newNode;
  31. size++;
  32. }
  33. void addAtTail(int val) {
  34. LinkedNode* cur = dummyHead;
  35. while(cur->next != nullptr)
  36. {
  37. cur = cur->next;
  38. }
  39. LinkedNode* newNode = new LinkedNode(val);
  40. cur->next= newNode;
  41. size++;
  42. }
  43. void addAtIndex(int index, int val) {
  44. if(index < 0 || index > size)
  45. return;
  46. LinkedNode* cur = dummyHead;
  47. LinkedNode* newNode = new LinkedNode(val);
  48. for(int i = 0; i < index; i++)
  49. {
  50. cur = cur->next;
  51. }
  52. newNode->next = cur->next;
  53. cur->next = newNode;
  54. size++;
  55. }
  56. void deleteAtIndex(int index)
  57. {
  58. if(index < 0 || index >= size)
  59. return;
  60. LinkedNode* cur = dummyHead;
  61. for(int i = 0; i < index; i++)
  62. {
  63. cur = cur->next;
  64. }
  65. cur->next = cur->next->next;
  66. size--;
  67. }
  68. private:
  69. int size;
  70. LinkedNode* dummyHead;
  71. };
  72. /**
  73. * Your MyLinkedList object will be instantiated and called as such:
  74. * MyLinkedList* obj = new MyLinkedList();
  75. * int param_1 = obj->get(index);
  76. * obj->addAtHead(val);
  77. * obj->addAtTail(val);
  78. * obj->addAtIndex(index,val);
  79. * obj->deleteAtIndex(index);
  80. */

206. 反转链表 - 力扣(LeetCode)

  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. class Solution {
  12. public:
  13. ListNode* reverseList(ListNode* head)
  14. {
  15. //反转链表主要是在反转的同时不能丢失后面的节点,统一操作可以使用一个指向nullptr的pre指针
  16. ListNode* pre = nullptr;
  17. ListNode* cur = head;
  18. while(cur)
  19. {
  20. ListNode* nNode = cur->next;
  21. cur -> next = pre;
  22. pre = cur;
  23. cur = nNode;
  24. }
  25. return pre;
  26. };
  27. };

24. 两两交换链表中的节点 - 力扣(LeetCode)

  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. class Solution {
  12. public:
  13. ListNode* swapPairs(ListNode* head) {
  14. if(!head || head->next == nullptr)
  15. return head;
  16. ListNode* dummyHead = new ListNode(0);
  17. dummyHead->next = head;
  18. ListNode* cur = head;
  19. ListNode* pre = dummyHead;
  20. while(cur != nullptr && cur->next != nullptr)
  21. {
  22. ListNode* next = cur->next;
  23. cur->next = next->next;
  24. next->next = pre->next;
  25. pre->next = next;
  26. pre = cur;
  27. cur = cur->next;
  28. }
  29. return dummyHead->next;
  30. }
  31. };

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

  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. class Solution {
  12. public:
  13. ListNode* removeNthFromEnd(ListNode* head, int n) {
  14. ListNode* dummyHead = new ListNode(0);
  15. dummyHead->next = head;
  16. //使用双指针用于定位到倒数第N个结点之前一个的节点
  17. ListNode* slow = dummyHead;
  18. ListNode* fast = dummyHead;
  19. //fast指针从dummyHead虚拟节点开始向前先走n+1步
  20. while(n-- && fast != nullptr)
  21. {
  22. fast = fast->next;
  23. }
  24. fast = fast->next;
  25. //然后快慢指针一起向前,到fast走到null后,slow所在节点的下一个节点就是要求被删除的节点
  26. while(fast)
  27. {
  28. slow = slow->next;
  29. fast = fast->next;
  30. }
  31. slow->next = slow->next->next;
  32. return dummyHead->next;
  33. }
  34. };

面试题 02.07. 链表相交 - 力扣(LeetCode)

  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 *getIntersectionNode(ListNode *headA, ListNode *headB) {
  12. ListNode* pointerA = headA;
  13. ListNode* pointerB = headB;
  14. while(pointerA != pointerB)
  15. {
  16. pointerA = pointerA != nullptr? pointerA->next: headB;
  17. pointerB = pointerB != nullptr? pointerB->next: headA;
  18. }
  19. return pointerA;
  20. }
  21. };

142. 环形链表 II - 力扣(LeetCode)

  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* slow = head;
  13. ListNode* fast = head;
  14. while(fast && fast->next != NULL)
  15. {
  16. fast = fast->next->next;
  17. slow = slow->next;
  18. if(fast == slow)
  19. {
  20. ListNode* index1 = head;
  21. ListNode* index2 = slow;
  22. while(index1 != index2)
  23. {
  24. index1 = index1->next;
  25. index2 = index2->next;
  26. }
  27. return index2;
  28. }
  29. }
  30. return NULL;
  31. }
  32. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/757146
推荐阅读
相关标签
  

闽ICP备14008679号