问题描述:

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 class Solution {
 2 public:
 3     ListNode *deleteDuplicates(ListNode *head) {
 4         if (head==NULL || head->next==NULL)
 5             return head;
 6
 7         ListNode *pDummyHead = new ListNode(0);
 8         pDummyHead->next = head;
 9
10         ListNode *pNewHead = head;
11         ListNode *p = head;
12         ListNode *pLast = pDummyHead;
13
14         bool bFoundHead = false;
15         while (p->next)
16         {
17             int nLastValue = p->val;
18             ListNode *pSub = p->next;
19             if (p->val != pSub->val)
20             {
21                 if (bFoundHead==false)
22                 {
23                     bFoundHead=true;
24                     pNewHead = p;
25                 }
26                 pLast = p;
27                 p = p->next;
28                 continue;
29             }
30             while (pSub && (pSub->val == nLastValue))
31             {
32                 ListNode *pToDelete = pSub;
33                 pSub = pSub->next;
34                 delete pToDelete;
35             }
36             ListNode *pToDelete = p;
37             p = pSub;
38             delete pToDelete;
39             pLast->next = pSub;
40             if (p==NULL)
41                 break;
42         }
43         if (bFoundHead==false)
44         {
45             pNewHead = p;
46         }
47         delete pDummyHead;
48         return pNewHead;
49     }
50 };

提交OJ,Accepted!

转载于:https://www.cnblogs.com/lequ/p/3885935.html

[leetcode笔记] Remove Duplicates from Sorted List II相关推荐

  1. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  2. LeetCode之Remove Duplicates from Sorted Array II

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

  3. 【Leetcode】Remove Duplicates from Sorted List II

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

  4. Leetcode 82. Remove Duplicates from Sorted List II

    利用一个虚拟头节点,和维护一个前置节点. # Definition for singly-linked list. # class ListNode: # def __init__(self, x): ...

  5. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

  6. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  7. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  8. [leetcode]83.Remove Duplicates from Sorted List

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

  9. [勇者闯LeetCode] 83. Remove Duplicates from Sorted List

    [勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...

最新文章

  1. 使用程序解决一道逻辑推理题
  2. react map循环生成的button_关于Vue和React的一些对比及个人思考(中)
  3. 循环的时候去删除集合中的元素 java.util.ConcurrentModificationException
  4. linux db2sysc 内存,db2sysc进程占用linux内存持续增长,请各位指点。
  5. controller属于哪一层_从数字一到十,在人们心中哪一个数字最为吉利?有什么原因?...
  6. linux查看虚拟化版本,4.15. 虚拟化 (机器翻译版本)
  7. UAC白名单解决每次启动都弹出UAC对话框(不用关闭UAC)
  8. 一文了解滴滴与蚂蚁金服开源共建的SQLFlow
  9. Apache POI Excel固定(冻结)单元格
  10. MT7921方案WIFI6无线网卡驱动编译方法
  11. 中国长租公寓与住房租赁领域未来八大趋势和创新探讨
  12. python对英雄皮肤进行图片采集~
  13. ExtJS教程(5)---Ext.data.Model之高级应用
  14. MyEclipse热部署----使用工具 JRebel
  15. 【Pyecharts | Map3D】带光影效果的3D地图 | 深圳地区二手房房价地图~
  16. 结对项目-小学生四则运算系统(GUI)
  17. 1.0 BS结构软件类型介绍
  18. 【T3】用友畅捷通特殊行业的期间损益结转设置
  19. Llinux系统下安装GNOME桌面
  20. 蚂蚁搬沙(2017佛山市选拔初中组)

热门文章

  1. ubuntu install opengl
  2. MySQL连接问题【如何解决MySQL连接超时关闭】
  3. Fedora 16 硬盘安装体会
  4. 《算法导论》(一)--插入排序与合并排序
  5. php二叉树 排列,PHP 如何实现用户二叉树排序需求
  6. leetcode算法题--比特位计数
  7. linux恢复出厂设置_怎么恢复tp-link路由器出厂设置 恢复tp-link出厂设置方法【详解】...
  8. 绘制简单的正太分布图
  9. 超越 一切还刚刚开始
  10. 《JavaScript面向对象精要》——第1章 原始类型和引用类型1.1 什么是类型