给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

  1. 输入: 1->2->3->3->4->4->5
  2. 输出: 1->2->5

示例 2:

  1. 输入: 1->1->1->2->3
  2. 输出: 2->3
  3.  
  4. 思路:(1)因为头结点可能被删除,所以要定义头结点;(2)定义两个指针,一个指向前驱结点,另一个指针指向前驱节的下一个结点,并不断找到值相等的结点。
  1. class Solution {
  2. public:
  3. ListNode* deleteDuplicates(ListNode* head) {
  4.  
  5. if(!head || !head->next)
  6. return head;
  7.  
  8. ListNode* dummy = new ListNode(-);
  9.  
  10. ListNode* p = dummy;
  11. dummy->next = head;
  12. while(p->next)
  13. {
  14. ListNode* cur = p->next;
  15.  
  16. while(cur->next && cur->val == cur->next->val)
  17. cur = cur->next;
  18.  
  19. if(cur != p->next)
  20. p->next = cur->next;
  21. else
  22. p = p->next;
  23.  
  24. }
  25.  
  26. return dummy->next;
  27. }
  28. };
  1.  

Remove Duplicates from Sorted ListII的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

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

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】

    题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...

  2. 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988

    题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...

  3. android 内存回收及怎样避免内存泄露

    http://blog.vunso.com/201307/android%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6%E5%8F%8A% ...

  4. css哪些属性可以继承

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  5. java解压缩zip

    依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...

  6. .Net core 使用特性Attribute验证Session登陆状态

    1.新建一个.net core mvc项目 2.在Models文件夹下面添加一个类MyAttribute,专门用来保存我们定义的特性 在这里我只写了CheckLoginAttribute用来验证登陆情 ...

  7. JAVA百度过的异常(1)

    1.---无法解析类型 javax.servlet.http.HttpServletRequest.从必需的 .class 文件间接引用了它 The type javax.servlet.http.H ...

  8. 【Convex Optimization (by Boyd) 学习笔记】Chapter 1 - Mathematical Optimization

    以下笔记参考自Boyd老师的教材[Convex Optimization]. I. Mathematical Optimization 1.1 定义 数学优化问题(Mathematical Optim ...

  9. com.nostra13.universalimageloader 加载displayImage图片时图片模糊的处理办法

    配置显示参数: DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(defaultR ...

  10. SQL Server - 哈希索引

    转载自:https://blog.csdn.net/josjiang1/article/details/80637076 作者:josjiang1 ————————总结———————— 使用场景: 1 ...