0


【数据结构】链表(leetcode)

目录


f65f2847c0974f8690bdeca8d6867a0f.gif


① 203.移除链表元素

d489e57558fa48ac852b2823a86dffa2.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. typedef struct ListNode ListNode;
  9. struct ListNode* removeElements(struct ListNode* head, int val) {
  10. ListNode*newHead = NULL, *newTail = NULL;
  11. ListNode*pcur = head;
  12. while(pcur != NULL)
  13. {
  14. if(pcur->val != val)
  15. {
  16. if(newHead == NULL)
  17. {
  18. //将新链表的头尾指针指向原链表头节点
  19. newHead = newTail = pcur;
  20. }
  21. else
  22. {
  23. newTail->next = pcur;
  24. newTail = newTail->next;
  25. }
  26. }
  27. pcur = pcur->next;
  28. }
  29. if(newTail != NULL)
  30. {
  31. newTail->next = NULL;
  32. }
  33. return newHead;
  34. }

② 206.反转链表

39aa95d0fe0944d18f93268d49f5a543.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode* reverseList(struct ListNode* head) {
  9. struct ListNode* prev = NULL;//哨兵位
  10. struct ListNode* cur = head;//头节点
  11. while (cur)
  12. {
  13. // 哨兵位(prev) 节点1(cur) 节点2(cur->next)
  14. struct ListNode* next = cur->next;//创建一个中间节点
  15. //开始改变链表的方向
  16. cur->next = prev;//节点2先指向节点1的前一个节点
  17. prev = cur;//哨兵位往后移动
  18. cur = next;//节点1向后移动
  19. }
  20. return prev;
  21. }

③ 876.链表的中间节点

f316c4e6c2dd4673b9b27bc84ad73646.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode* middleNode(struct ListNode* head) {
  9. //慢指针(一次走一步)
  10. struct ListNode* slow = head;
  11. //快指针(一次走两步)
  12. struct ListNode* fast = head;
  13. while(fast && fast->next){
  14. slow = slow->next;
  15. fast = fast->next->next;
  16. }
  17. return slow;
  18. }

④ 返回倒数第k个节点(面试题)

7d8c78d2f96c4b049b90362c8144706b.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. int kthToLast(struct ListNode* head, int k) {
  9. struct ListNode *fast = head, *slow = head;
  10. //本题采用的是相对法:
  11. // fast先运动k个节点
  12. // 假设该链表的节点个数是 m+k 个
  13. // 则先走k个节点,剩下fast指针到null指针时,即走了m个节点
  14. // 此时,slow指针就剩余k个节点
  15. while(k--){
  16. fast = fast->next;
  17. }
  18. while(fast != NULL){
  19. slow = slow->next;
  20. fast = fast->next;
  21. }
  22. return slow->val;
  23. }

⑤ 21.合并两个有序链表

33bda6c74a77458eae7845409d991cd1.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  9. if(list1 == NULL)
  10. {
  11. return list2;
  12. }if(list2 == NULL)
  13. {
  14. return list1;
  15. }
  16. struct ListNode* l1 = list1;
  17. struct ListNode* l2 = list2;
  18. struct ListNode* newHead, *newTail;
  19. newHead = newTail = NULL;
  20. while(l1 && l2)
  21. {
  22. //比大小
  23. if(l1->val < l2->val)
  24. {
  25. if(newHead == NULL)
  26. {
  27. newHead = newTail = l1;
  28. }
  29. else
  30. {
  31. newTail->next = l1;
  32. newTail = newTail->next;
  33. }
  34. l1 = l1->next;
  35. }
  36. else
  37. {
  38. if(newHead == NULL)
  39. {
  40. newHead = newTail = l2;
  41. }else
  42. {
  43. newTail->next = l2;
  44. newTail = newTail->next;
  45. }
  46. l2 = l2->next;
  47. }
  48. }
  49. if(l1)
  50. {
  51. newTail->next = l1;
  52. }
  53. if(l2)
  54. {
  55. newTail->next = l2;
  56. }
  57. return newHead;
  58. }

⑥ 160.相交链表

ec919a9804a74894a852973faae88385.png

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
  9. struct ListNode* curA = headA, *curB = headB;
  10. int lenA = 1, lenB = 1;
  11. while(curA->next){
  12. curA = curA->next;
  13. ++lenA;
  14. }
  15. while(curB->next){
  16. curB = curB->next;
  17. ++lenB;
  18. }
  19. if(curA != curB){
  20. return NULL;
  21. }
  22. //假设法
  23. int gap = abs(lenA - lenB);
  24. struct ListNode* longList = headA, *shortList = headB;
  25. if(lenB > lenA){
  26. longList = headB;
  27. shortList = headA;
  28. }`
  29. while(gap--){
  30. longList = longList->next;
  31. }
  32. while(longList != shortList){
  33. longList = longList->next;
  34. shortList = shortList->next;
  35. }
  36. return longList;
  37. }

⑦ 138.随机链表的复制(深拷贝)

1369640e4ebc41a29064d21f90ee31cf.png

53510b52c9804686aa1fe9e98b51005d.png

  1. /**
  2. * Definition for a Node.
  3. * struct Node {
  4. * int val;
  5. * struct Node *next;
  6. * struct Node *random;
  7. * };
  8. */
  9. struct Node* copyRandomList(struct Node* head) {
  10. struct Node* cur = head;
  11. while(cur){
  12. struct Node * copy = (struct Node*)malloc(sizeof(struct Node));
  13. //赋值
  14. copy->val = cur->val;
  15. //将copy链表插入原链表中
  16. copy->next = cur->next;
  17. cur->next = copy;
  18. //cur向后移动
  19. cur = copy->next;
  20. }
  21. //将copy链表中random指针的指向与原链表中的指针对调
  22. cur = head;
  23. while(cur){
  24. struct Node* copy = cur->next;
  25. if(cur->random == NULL){
  26. copy->random = NULL;
  27. }
  28. else{
  29. copy->random = cur->random->next;
  30. }
  31. cur = copy->next;
  32. }
  33. cur = head;
  34. struct Node* copyHead = NULL,* copyTail =NULL;
  35. while(cur){
  36. struct Node* copy = cur->next;
  37. if(copyTail == NULL)
  38. {
  39. copyHead = copyTail = copy;
  40. }else
  41. {
  42. copyTail->next = copy;
  43. copyTail = copyTail->next;
  44. }
  45. //cur向后移动
  46. cur = copy->next;
  47. }
  48. return copyHead;
  49. }

f1676cb2fcf24d61b464dfe0a9decfdd.gif


本文转载自: https://blog.csdn.net/2301_81348661/article/details/143726000
版权归原作者 爱摸鱼的孔乙己 所有, 如有侵权,请联系我们删除。

“【数据结构】链表(leetcode)”的评论:

还没有评论