当前位置:   article > 正文

【数据结构和算法初阶(C语言)】顺序表+单链表经典例题图文详解(题解大合集,搭配图文演示详解,一次吃饱吃好)_顺序表c语言练习

顺序表c语言练习

目录

 1.移除链表元素

1.1思路1:遍历删除 

1. 2  思路2:尾插法 

2.反转链表 

3.链表的中间节点 

 3.1解题思想及过程

3.2快慢指针思想解题---变式:返回链表的倒数第K个节点 

4.合并两个有序链表

4.1解题思想 1取小的尾插

5.反转链表

6、CM11 链表分割 (比较难)

描述

7.OR36 链表的回文结构

8.相交链表 

9.结语


 1.移除链表元素

1203. 移除链表元素icon-default.png?t=N7T8https://leetcode.cn/problems/remove-linked-list-elements/

 

1.1思路1:遍历删除 

  1. struct ListNode* removeElements(struct ListNode* head, int val) {
  2. if(head==NULL)
  3. {
  4. return NULL;
  5. }
  6. struct ListNode* cur = head;
  7. struct ListNode* pre = head;
  8. while(cur->next)
  9. {
  10. if(cur->val==val)
  11. {
  12. //删除
  13. //如果是头结点
  14. if(head == cur)
  15. {
  16. head = cur->next;
  17. free(cur);
  18. cur = head;
  19. pre = cur;
  20. }
  21. else
  22. {
  23. pre->next = cur->next;
  24. free(cur);
  25. cur = pre->next;
  26. }
  27. }
  28. else
  29. {
  30. pre= cur;
  31. cur = cur->next;
  32. }
  33. }
  34. //处理尾巴
  35. if(cur->val == val)
  36. {
  37. if(head == cur)
  38. {
  39. free(cur);
  40. return NULL;
  41. }
  42. else{
  43. pre->next = cur ->next;
  44. free(cur);
  45. }
  46. }
  47. return head;
  48. }

1. 2  思路2:尾插法 

  1. struct ListNode* removeElements(struct ListNode* head, int val) {
  2. // if(head==NULL)
  3. // {
  4. // return NULL;
  5. // }
  6. // struct ListNode* cur = head;
  7. // struct ListNode* pre = head;
  8. // while(cur->next)
  9. // {
  10. // if(cur->val==val)
  11. // {
  12. // //删除
  13. // //如果是头结点
  14. // if(head == cur)
  15. // {
  16. // head = cur->next;
  17. // free(cur);
  18. // cur = head;
  19. // pre = cur;
  20. // }
  21. // else
  22. // {
  23. // pre->next = cur->next;
  24. // free(cur);
  25. // cur = pre->next;
  26. // }
  27. // }
  28. // else
  29. // {
  30. // pre= cur;
  31. // cur = cur->next;
  32. // }
  33. // }
  34. // //处理尾巴
  35. // if(cur->val == val)
  36. // {
  37. // if(head == cur)
  38. // {
  39. // free(cur);
  40. // return NULL;
  41. // }
  42. // else{
  43. // pre->next = cur ->next;
  44. // free(cur);
  45. // }
  46. // }
  47. // return head;
  48. struct ListNode* cur = head;//用于遍历链表
  49. struct ListNode* newhead = NULL;//创建新的头结点
  50. struct ListNode* tail = NULL;//定义一个尾指针
  51. //开始遍历原先的链表
  52. while(cur)
  53. {
  54. if(cur->val == val)
  55. {
  56. struct ListNode* deal = cur;//记录一下要删除的节点
  57. cur = cur->next;
  58. free(deal);
  59. }
  60. else
  61. {
  62. if(tail == NULL)//第一次插入
  63. {
  64. tail = cur;
  65. newhead = cur;
  66. }
  67. else
  68. {
  69. tail ->next = cur;
  70. tail = tail ->next;
  71. }
  72. cur = cur->next;
  73. }
  74. //单独处理一下尾巴最后一个节点要删除的话,我们的这个尾节点要为空
  75. if(tail)
  76. {
  77. tail->next = NULL;
  78. }
  79. }
  80. return newhead;
  81. }

 

2.反转链表 

206. 反转链表icon-default.png?t=N7T8https://leetcode.cn/problems/reverse-linked-list/ 放在第五讲解

3.链表的中间节点 

. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/middle-of-the-linked-list/

 3.1解题思想及过程

这道题最简单的方法就是两次遍历的方法,第一次遍历就可以得到链表的长度,从而求得链表的的中间位置,第二次遍历返回中间节点就好。今天重点讲解一次遍历方法使用快慢指针

偶数长度的链表遍历结束的条件是:快指针指向空

奇数链表长度的遍历结束的条件是:快指针指向的下一个位置为空 

  1. struct ListNode* middleNode(struct ListNode* head) {
  2. //快慢指针
  3. struct ListNode* slow = head;
  4. struct ListNode* fast = head;
  5. if(head == NULL)//传入空指针
  6. {
  7. return NULL;
  8. }
  9. while(fast&&fast->next)
  10. {
  11. slow = slow->next;
  12. fast = fast->next->next;
  13. }
  14. return slow;
  15. }

 

3.2快慢指针思想解题---变式:返回链表的倒数第K个节点 

解题思想:快慢指针

快指针先走K步,然后和慢指针一起移动,直到快指针来到尾节点的下一个位置过后,慢指针指向的位置就是我们的倒数第k个节点。

特别注意就是:如果我们传入空链表或者我们的输入的倒数第几个的数据,但是链表没有那么长的时候,应该返回空。 

  1. struct ListNode* FindKthToTail(struct Listnode* plistHead, int k)
  2. {
  3. //首先判断一下链表为不为空,为空就返回NULL
  4. if (plistHead == NULL)
  5. {
  6. return NULL;
  7. }
  8. struct ListNode* slow, * fast;
  9. slow = fast = plistHead;
  10. //先让快指针走K步
  11. while (k--)
  12. {
  13. //但是要注意判断如果我们的链表没有K步长,就返回空
  14. if (fast == NULL)
  15. {
  16. return NULL;
  17. }
  18. fast = fast->next;
  19. }
  20. //然后快慢指针一起走,直到快指针走到空
  21. while (fast)
  22. {
  23. slow = slow->next;
  24. fast = fast->next;
  25. }
  26. return slow;
  27. }

 

4.合并两个有序链表

21. 合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

4.1解题思想 1取小的尾插

主要是尾插思想,由于是有序链表,我们就取小的尾差到新的头结点,当一个链表或者两个链表同时走完就结束,将另外一个链表剩下的所有元素尾插到新的链表中然后返回头结点就可以。为了不增加遍历的时间复杂度,还是定义一个尾指针。

 

当有一个链表先走完时:
 

  1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  2. struct ListNode* head = NULL;//定义一个头指针
  3. struct ListNode* tail = NULL;//定义一个尾指针
  4. //先判断传入的链表中1是否有空链表,有就返回另外一个
  5. if(list1 == NULL)
  6. {
  7. return list2;
  8. }
  9. if(list2==NULL)
  10. {
  11. return list1;
  12. }
  13. while(list1 && list2)
  14. {
  15. if(list1->val< list2->val)//取小的尾插
  16. {
  17. if(tail == NULL)//判断是否是第一次插入
  18. {
  19. tail = head = list1;
  20. list1 = list1->next;
  21. }
  22. else
  23. {
  24. tail->next= list1;
  25. list1 = list1->next;
  26. tail = tail->next;
  27. }
  28. }
  29. else
  30. {
  31. if(tail == NULL)//判断是否是第一次插入
  32. {
  33. tail = head = list2;
  34. list2 = list2->next;
  35. }
  36. else
  37. {
  38. tail->next= list2;
  39. tail = tail->next;
  40. list2 = list2->next;
  41. }
  42. }
  43. }
  44. //出循环有一个链表为空,就要判断一下是哪一个为空,然后将另外一个进行尾插
  45. if(list1)
  46. {
  47. tail->next = list1;
  48. }
  49. if(list2)
  50. {
  51. tail->next = list2;
  52. }
  53. return head;
  54. }

 

5.反转链表

. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/reverse-linked-list/ 

 

 解题思想:头插法

 

 

  1. struct ListNode* reverseList(struct ListNode* head) {
  2. //先判断一下传入的链表是否为空
  3. if(head == NULL)
  4. {
  5. return NULL;
  6. }
  7. struct ListNode* newhead = NULL;
  8. struct ListNode* cur = head;
  9. struct ListNode* after=NULL;
  10. while(cur)
  11. {
  12. after = cur->next;
  13. cur ->next = newhead;
  14. newhead = cur;
  15. cur = after;
  16. }
  17. return newhead;
  18. }

 

6、CM11 链表分割 (比较难)

描述

现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

做题链接

补充带哨兵位的链表

 

首先解题的整体思想:
大于等于X的放置在一个链表,使用尾插的办法为了不改变顺序

小于X的放置在一个链表,也使用尾插的办法。

最后将两个链表链接起来。

这里使用带哨兵位的链表。我们先看一下代码实现,一边走一边说明问题

 

 

 

那么我们应该将大于数据的链表的尾巴置空。 

  1. class Partition {
  2. public:
  3. ListNode* partition(ListNode* pHead, int x) {
  4. // write code here
  5. struct ListNode* lhead ,*ltail;//定义小于X的值存放的链表的头尾节点
  6. struct ListNode* ghead ,*gtail;//定义大于X的值存放的链表的头尾节点
  7. //申请哨兵位空间
  8. lhead = ltail = ( struct ListNode*)malloc(sizeof( struct ListNode));
  9. ghead = gtail = ( struct ListNode*)malloc(sizeof( struct ListNode));
  10. //开始循环遍历找
  11. struct ListNode* cur= pHead;
  12. while(cur)
  13. {
  14. if(cur->val <x)
  15. {
  16. ltail ->next = cur;
  17. ltail = ltail ->next;
  18. cur = cur->next;
  19. }
  20. else {
  21. gtail->next = cur;
  22. gtail = gtail->next;
  23. cur = cur->next;
  24. }
  25. }
  26. //链接
  27. ltail->next = ghead->next;
  28. gtail->next = NULL;
  29. free(ghead);
  30. struct ListNode* head = lhead;
  31. head = head ->next;
  32. free(lhead);
  33. return head;
  34. }
  35. };

7.OR36 链表的回文结构

题目地址

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。 测试样例: 1-2-2-1 返回:true

  1. struct ListNode* reverseList(struct ListNode* head) {
  2. //先判断一下传入的链表是否为空
  3. if (head == NULL)
  4. {
  5. return NULL;
  6. }
  7. struct ListNode* newhead = NULL;
  8. struct ListNode* cur = head;
  9. struct ListNode* after = NULL;
  10. while (cur)
  11. {
  12. after = cur->next;
  13. cur->next = newhead;
  14. newhead = cur;
  15. cur = after;
  16. }
  17. return newhead;
  18. }
  19. struct ListNode* middleNode(struct ListNode* head) {
  20. //快慢指针
  21. struct ListNode* slow = head;
  22. struct ListNode* fast = head;
  23. if (head == NULL)//传入空指针
  24. {
  25. return NULL;
  26. }
  27. while (fast && fast->next)
  28. {
  29. slow = slow->next;
  30. fast = fast->next->next;
  31. }
  32. return slow;
  33. }
  34. bool chkPAlindrome(ListNode* head)
  35. {
  36. struct ListNode* mid = middleNode(head);
  37. struct LIstNOde* rmid = reverselist(mid);
  38. while (rmid && head)
  39. {
  40. struct ListNode* scur = head;
  41. if(rmid->val != scur ->val)
  42. return false;
  43. rmid = rmid->next;
  44. scur = head->next;
  45. }
  46. }

8.相交链表 

160. 相交链表

 

  1. struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
  2. struct ListNode * cura = headA,*curb = headB;
  3. int lena = 1;
  4. int lenb = 1;
  5. while(cura->next)
  6. {
  7. cura = cura->next;
  8. lena++;//计算链表a的长度
  9. }
  10. while(curb->next)
  11. {
  12. curb = curb->next;
  13. lenb++;//计算链表b的长度
  14. }
  15. //如果两个链表相交,那么尾结点的地址一定相等
  16. //所以这里就可以判断一下两个链表是否相交
  17. if(cura != curb)
  18. {
  19. return NULL;
  20. }
  21. int gap = abs(lena-lenb);//计算两个链表之间的差值,用来绝对值
  22. struct ListNode*longst = headA,*shortlist = headB;
  23. if(lena<lenb)
  24. {
  25. longst = headB;
  26. shortlist = headA;
  27. }
  28. while(gap--)
  29. {
  30. longst = longst->next;//长的先走差距步
  31. }
  32. //同时找交点
  33. while(longst != shortlist)
  34. {
  35. longst = longst->next;
  36. shortlist = shortlist->next;
  37. }
  38. return longst;
  39. }

 

9.结语

今天关于简单链表的题目解析就更新到这里,下几篇内容是比较复杂的带环链表和复杂连边的题目,大家可以先收藏或者关注一波,方便链接后续新解 

 

 

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

闽ICP备14008679号