Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

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

思路:

常规思路,关键点:用伪头部避免新建链表头的麻烦。

  1. ListNode *deleteDuplicates2(ListNode *head) {
  2. if(head == NULL || head->next == NULL) return head;
  3. ListNode fakehead(); //伪头结点 避免头结点建立的好方法
  4. ListNode * newtail = &fakehead;
  5. ListNode * p = head;
  6. int preval = head->val + ; //记录前面结点的值 初始化为一个跟头结点值不同的数
  7.  
  8. while(p != NULL)
  9. {
  10. if(p->val == preval) //数字出现过
  11. {
  12. p = p->next;
  13. }
  14. else //新数字
  15. {
  16. preval = p->val;
  17. if(p->next != NULL && p->next->val == preval) //新数字的下一个数字还是这个数 跳过
  18. {
  19. p = p->next->next;
  20. }
  21. else //如果确定是一个独立的数字
  22. {
  23. newtail->next = p;
  24. p = p->next;
  25. newtail = newtail->next;
  26. newtail->next = NULL;
  27. }
  28. }
  29. }
  30. return fakehead.next;
  31. }

大神不需要伪头部的方法,利用了指针的地址。直接更改地址里面放的指针。

  1. class Solution {
  2. public:
  3. ListNode *deleteDuplicates(ListNode *head) {
  4. ListNode** p = &head;
  5. while(*p && (*p)->next) {
  6. ListNode* p1 = *p, *p2 = p1->next;
  7. while(p2 && p2->val == p1->val) { //直接循环到下个数字出现的位置
  8. p2 = p2->next;
  9. }
  10. if(p2 != p1->next) {
  11. *p = p2;
  12. } else {
  13. p = &(p1->next);
  14. }
  15. }
  16. return head;
  17. }
  18. };

【leetcode】Remove Duplicates from Sorted List II (middle)的更多相关文章

  1. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【Leetcode】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 【链表】Remove Duplicates from Sorted List II(三指针)

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  5. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  6. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  9. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. 解析posix与perl标准的正则表达式区别 ---PHP

        正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式.正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系 ...

  2. C#创建windows服务列表

    转载自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html Windows Service这一块并不复杂,但是注意事项太多了,网上 ...

  3. UI第四节——UIImageView详解

    - (void)viewDidLoad { // super调用是必须的 [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@&quo ...

  4. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. java执行顺序

    本文讨论Java中(静态)变量.(静态)代码块的执行顺序 首先创建3个类: 1.Foo类,用于打印变量 public class Foo { public Foo(String word) { Sys ...

  6. JS中的prototype(原文地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html)

    JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

  7. FineUI第十四天---布局之垂直布局和水平布局

    布局值水平布局和垂直布局 垂直盒子布局和水平盒子布局非常灵活易用,在很大程度上能够取代锚点布局,行布局和列布局. 1.垂直盒子布局: BoxConfigAlign:控制子容器的的尺寸 Start:位于 ...

  8. 带你走进rsync的世界

    导读 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录.rsync共有3种使用方 ...

  9. UIMenuController使用

    - (void)bubbleDidLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { if(gestureRecognizer. ...

  10. phpstorm的调试工具xdebug

    1.需求 知道xdebug的使用方法 2.安装xdebug http://www.awaimai.com/1290.html 3.配置phpstorm http://www.awaimai.com/1 ...