LeetCode之Remove Duplicates from Sorted List

举报
chenyu 发表于 2021/07/27 00:57:41 2021/07/27
【摘要】 1、题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1-&gt...

1、题目

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

1->2->2->2 return 1->2

1->2->3->3 return 1->3


2、代码实现


   
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. struct List_Node {
  5. int val;
  6. struct List_Node *next;
  7. };
  8. struct List_Node* deleteDuplicates(struct List_Node* head) {
  9. }
  10. void show_list (struct List_Node* head) {
  11. while (head != NULL) {
  12. printf("val:%d\n", head->val);
  13. head = head->next;
  14. }
  15. }
  16. struct List_Node* create_head1(){
  17. struct List_Node *p1, *p2, *p3, *p4, *p5;
  18. p1 = (struct List_Node *)malloc(sizeof(p1));
  19. p2 = (struct List_Node *)malloc(sizeof(p2));
  20. p3 = (struct List_Node *)malloc(sizeof(p3));
  21. p4 = (struct List_Node *)malloc(sizeof(p4));
  22. p5 = (struct List_Node *)malloc(sizeof(p5));
  23. p1->val = 1;
  24. p2->val = 1;
  25. p3->val = 2;
  26. p4->val = 3;
  27. p5->val = 3;
  28. p1->next = p2;
  29. p2->next = p3;
  30. p3->next = p4;
  31. p4->next = p5;
  32. p5->next = NULL;
  33. return p1;
  34. }
  35. struct List_Node* create_head2(){
  36. struct List_Node *p1, *p2, *p3, *p4, *p5;
  37. p1 = (struct List_Node *)malloc(sizeof(p1));
  38. p2 = (struct List_Node *)malloc(sizeof(p2));
  39. p3 = (struct List_Node *)malloc(sizeof(p3));
  40. p4 = (struct List_Node *)malloc(sizeof(p4));
  41. p5 = (struct List_Node *)malloc(sizeof(p5));
  42. p1->val = 1;
  43. p2->val = 2;
  44. p3->val = 2;
  45. p4->val = 2;
  46. p5->val = 2;
  47. p1->next = p2;
  48. p2->next = p3;
  49. p3->next = p4;
  50. p4->next = p5;
  51. p5->next = NULL;
  52. return p1;
  53. }
  54. struct List_Node* create_head3(){
  55. struct List_Node *p1, *p2, *p3, *p4, *p5;
  56. p1 = (struct List_Node *)malloc(sizeof(p1));
  57. p2 = (struct List_Node *)malloc(sizeof(p2));
  58. p3 = (struct List_Node *)malloc(sizeof(p3));
  59. p4 = (struct List_Node *)malloc(sizeof(p4));
  60. p5 = (struct List_Node *)malloc(sizeof(p5));
  61. p1->val = 1;
  62. p2->val = 2;
  63. p3->val = 3;
  64. p4->val = 3;
  65. p5->val = 3;
  66. p1->next = p2;
  67. p2->next = p3;
  68. p3->next = p4;
  69. p4->next = p5;
  70. p5->next = NULL;
  71. return p1;
  72. }
  73. int main()
  74. {
  75. struct List_Node *head = create_head3();
  76. show_list(head);
  77. puts("after");
  78. struct List_Node *p = head;
  79. struct List_Node *q = head->next;
  80. while (q != NULL) {
  81. if (p->val == q->val) {
  82. p->next = q->next;
  83. } else {
  84. p = q; //注意这里已经是head = head.next
  85. // p = p.next;
  86. }
  87. q = q->next;
  88. }
  89. show_list(head);
  90. return 0;
  91. }

 
 


3、总结

比如1->1->2->3->3
我们这样分析
第一:需要搞个指针从第二个元素往后移动和前面一个指针指向下一个元素对比,如果前后2个指针的值一样,那么前面的指针需要指向下一个元素的下一个元素
第二:每对比一步,需要第二个指针往后移动
第三:只有当前元素和下面一个元素的下面一个元素不相等,我们就把他们连接起来,也就是走head = head.next;
过程如下
1->1
1->2 (head = head.next)
1->2->3(head = head.next)
1->2->3->null 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/66974159

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。