当前位置:   article > 正文

Leetcode刷题:链表

Leetcode刷题:链表

203移除链表元素

使用原来的链表来进行移除节点

关键点在于对头节点的处理

  1. struct ListNode {
  2. int val;
  3. ListNode *next;
  4. ListNode() : val(0), next(nullptr) {}
  5. ListNode(int x) : val(x), next(nullptr) {}
  6. ListNode(int x, ListNode *next) : val(x), next(next) {}
  7. };
  8. class Solution {
  9. public:
  10. ListNode* removeElements(ListNode* head, int val) {
  11. while(head!=NULL && head->val == val) {
  12. ListNode* temp = head;
  13. head = head->next;
  14. delete temp;
  15. }
  16. ListNode *cur = head;
  17. while(cur!=NULL && cur->next!=NULL) {
  18. if(cur->next->val == val) {
  19. ListNode* temp = cur->next;
  20. cur -> next = cur->next->next;
  21. delete temp;
  22. }else {
  23. cur = cur->next;
  24. }
  25. }
  26. return head;
  27. }
  28. };

使用虚拟节点当作头节点

思路就是自己造一个虚拟节点避免掉要再考虑头节点的值=target值的特殊情况

  1. class Solution2 {
  2. public:
  3. ListNode* removeElements(ListNode* head, int val) {
  4. ListNode vir(val+1,head);
  5. ListNode* cur = &vir;
  6. while(cur!=NULL && cur->next!=NULL) {
  7. if(cur->next->val == val) {
  8. ListNode* temp = cur->next;
  9. cur->next = cur->next->next;
  10. delete temp;
  11. }else {
  12. cur = cur->next;
  13. }
  14. }
  15. return vir.next;
  16. }
  17. };

707设计链表

  1. class MyLinkedList {
  2. public:
  3. struct Linked_node {
  4. int val;
  5. Linked_node* next;
  6. Linked_node(int x,Linked_node* next):val(x),next(next){};
  7. Linked_node(int x):val(x),next(NULL){};
  8. };
  9. MyLinkedList() {
  10. _dummyHead = new Linked_node(0);
  11. _size = 0;
  12. }
  13. int get(int index) {
  14. if(index>0 && index<_size) {
  15. Linked_node* temp = _dummyHead->next;
  16. for(int i=0;i<index;i++) {
  17. temp = temp->next;
  18. }
  19. return temp->val;
  20. }
  21. else return -1;
  22. }
  23. void addAtHead(int val) {
  24. Linked_node* temp = new Linked_node(val,_dummyHead->next);
  25. _dummyHead->next = temp;
  26. _size ++;
  27. }
  28. void addAtTail(int val) {
  29. Linked_node * temp = new Linked_node(val,NULL);
  30. Linked_node * cur = _dummyHead;
  31. while(cur->next != NULL) {
  32. cur = cur->next;
  33. }
  34. cur->next = temp;
  35. _size++;
  36. }
  37. void addAtIndex(int index, int val) {
  38. if(index>_size) return;
  39. if(index<0) index = 0;
  40. Linked_node *temp = new Linked_node(val);
  41. Linked_node *cur = _dummyHead;
  42. for(int i=0;i<index;i++) {
  43. cur = cur->next;
  44. }
  45. temp->next = cur->next;
  46. cur->next = temp;
  47. _size++;
  48. }
  49. void deleteAtIndex(int index) {
  50. Linked_node* cur = _dummyHead;
  51. for(int i=0;i<index;i++) {
  52. cur = cur->next;
  53. }
  54. Linked_node* temp = cur->next;
  55. cur->next = cur->next->next;
  56. delete temp;
  57. _size--;
  58. }
  59. private:
  60. int _size;
  61. Linked_node* _dummyHead;
  62. };

206反转链表

双指针

  1. //双指针法
  2. ListNode* reverseList(ListNode* head) {
  3. ListNode *pre = NULL;
  4. ListNode *cur = head;
  5. ListNode *temp ;
  6. while(cur) {
  7. temp = cur->next;
  8. cur->next = pre;
  9. pre = cur;
  10. cur = temp;
  11. }
  12. return cur;
  13. }

递归法

  1. ListNode *reverse(ListNode* cur,ListNode* pre){
  2. if(cur == NULL) return pre;
  3. ListNode* temp = cur->next;
  4. cur->next = pre;
  5. pre = cur;
  6. cur = temp;
  7. return reverse(cur,pre);
  8. }
  9. ListNode *reverseList1(ListNode* head) {
  10. return reverse(head,NULL);
  11. }

24两两交换链表中的元素

虚拟头节点(画图就懂)

  1. ListNode* swapPairs(ListNode* head) {
  2. ListNode *dummy_node = new ListNode(0,head);
  3. ListNode *cur = dummy_node;
  4. ListNode* temp;
  5. ListNode* temp1;
  6. while(cur->next!=NULL && cur->next->next!=NULL) {
  7. temp = cur->next;
  8. temp1 =cur->next->next->next;
  9. cur->next = cur->next->next;
  10. cur -> next->next = temp;
  11. temp->next = temp1;
  12. cur = cur->next->next;
  13. }
  14. ListNode *result = dummy_node->next;
  15. return result;
  16. };

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

双链表法

  1. //双指针
  2. ListNode* removeNthFromEnd(ListNode* head, int n) {
  3. ListNode* dummyhead = new ListNode(0,head);
  4. ListNode* fastIndex = dummyhead;
  5. for(int i=0;i<n;i++) {
  6. fastIndex = fastIndex->next;
  7. }
  8. if (fastIndex==NULL)
  9. return dummyhead->next;
  10. ListNode* slowindex = dummyhead;
  11. while(fastIndex->next!=NULL) {
  12. fastIndex = fastIndex->next;
  13. slowindex = slowindex->next;
  14. }
  15. delete slowindex->next;
  16. slowindex->next = slowindex->next->next;
  17. return dummyhead->next;
  18. }

面试题02.07链表相交

列表尾部对齐

  1. //列表尾部对齐 我的写法
  2. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  3. if(headA==NULL||headB==NULL)return NULL;
  4. ListNode* a=headA;
  5. ListNode* b=headB;
  6. ListNode* slowIndex;
  7. ListNode* shortNode;
  8. while(a->next!=NULL && b->next!=NULL) {
  9. a = a->next;
  10. b = b->next;
  11. }
  12. if(a->next == NULL) {
  13. shortNode = headA;
  14. slowIndex = headB;
  15. if(b->next != NULL) {
  16. while(b->next!=NULL) {
  17. b = b->next;
  18. slowIndex = slowIndex->next;
  19. }
  20. }
  21. }else {
  22. shortNode = headB;
  23. slowIndex = headA;
  24. if(a->next != NULL) {
  25. while(a->next!=NULL) {
  26. a = a->next;
  27. slowIndex = slowIndex->next;
  28. }
  29. }
  30. }
  31. while(slowIndex!=NULL ) {
  32. if(slowIndex == shortNode) {
  33. return slowIndex;
  34. }else {
  35. slowIndex = slowIndex->next;
  36. shortNode = shortNode->next;
  37. }
  38. }
  39. return NULL;
  40. }

142环形链表

这个题解题思路还是挺有意思的用双指针,快指针一次走两格,慢指针一次走一格。这个的证明用举例子就行,他们两个再相遇之前的几种情况举出来就行。然后根据下面这幅图可以列出两个指针时间相等的式子x+y = \frac{x+n(x+y)+y)}{2},化简得到x = (n-1)(y+z)+z这里n必是大于等于1的逻辑思考一下就知道了。根据得出来的式子可以知道,让一个指针位于起点一个指针位于相遇点,以每次一格的速度往前走,最后会在环形入口节点相遇。

  1. ListNode *detectCycle(ListNode *head) {
  2. if(head == NULL)return NULL;
  3. ListNode *fastP = head;
  4. ListNode *slowP = head;
  5. while(true) {
  6. if(fastP->next==NULL || fastP->next->next==NULL){return NULL;}
  7. else {
  8. fastP = fastP->next->next;
  9. slowP = slowP->next;
  10. }
  11. if(fastP == slowP) break;
  12. }
  13. slowP = head;
  14. while(slowP!=fastP) {
  15. fastP = fastP->next;
  16. slowP = slowP->next;
  17. }
  18. return slowP;
  19. }

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

闽ICP备14008679号