题目:

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

Example

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

题解:

Solution 1 ()

  1. class Solution {
  2. public:
  3. ListNode *deleteDuplicates(ListNode *head) {
  4. if (head == nullptr) {
  5. return head;
  6. }
  7. ListNode* dummy = new ListNode(-);
  8.  
  9. dummy->next = head;
  10. head = dummy;
  11. while (head->next != nullptr && head->next->next != nullptr) {
  12. if (head->next->val == head->next->next->val) {
  13. int value = head->next->val;
  14. while (head->next != nullptr && head->next->val == value) {
  15. head->next = head->next->next;
  16. }
  17. } else {
  18. head = head->next;
  19. }
  20. }
  21.  
  22. return dummy->next;
  23. }
  24. };

  二级指针

Solution 2 ()

  1. class Solution {
  2. public:
  3. ListNode *deleteDuplicates(ListNode *head) {
  4. if (head == nullptr || head->next == nullptr) {
  5. return head;
  6. }
  7. ListNode **t = &head;
  8. while (*t) {
  9. if ((*t)->next && (*t)->next->val == (*t)->val) {
  10. ListNode *tmp = *t;
  11. while (tmp && (*t)->val == tmp->val) {
  12. tmp = tmp->next;
  13. }
  14. *t = tmp;
  15. } else {
  16. t = &((*t)->next);
  17. }
  18. }
  19.  
  20. return head;
  21. }
  22. };

 

【Lintcode】113.Remove Duplicates from Sorted List II的更多相关文章

  1. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  2. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  3. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  4. 【Lintcode】112.Remove Duplicates from Sorted List

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

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告

    明日深圳行,心情紧张,写博文压压惊 囧 ------------------------------------- 原题地址: https://oj.leetcode.com/problems/rem ...

  7. 【Leetcode】82. Remove Duplicates from Sorted List II

    Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...

  8. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  9. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

随机推荐

  1. PHP实现上次登录功能

    通过一个sql语句把上次的登录时间给本次登录时间,再把当前时间记录下来 update userinfo  set lasttime=userinfo.logintime,logintime= CURR ...

  2. HDFS源码分析之DataXceiverServer

    DataXceiverServer是Hadoop分布式文件系统HDFS的从节点--数据节点DataNode上的一个后台工作线程,它类似于一个小型的服务器,被用来接收数据读写请求,并为每个请求创建一个工 ...

  3. windows 10 python 2.7和python3.6共存解决方法和pip安装

    一.首先去python官网将两个版本下载并安装: 然后进入windows的环境变量,检查下面4个变量: 1.C:\Python272.C:\Python27\Scripts3.D:\software\ ...

  4. 九度OJ 1021:统计字符 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5418 解决:3146 题目描述:     统计一个给定字符串中指定的字符出现的次数. 输入:     测试输入包含若干测试用例,每个测试用 ...

  5. 连通性 保证f具有介值性质

  6. Swift 学习笔记(面向协议编程)

    在Swift中协议不仅可以定义方法和属性,而且协议是可以扩展的,最关键的是,在协议的扩展中可以添加一些方法的默认实现,就是在协议的方法中可以实现一些逻辑,由于这个特性,Swift是可以面向协议进行编程 ...

  7. 前端面试题(一)JS篇

    内置类型 JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: null,undefined,boolean,number,string,symbo ...

  8. 【题解】P2048 [NOI2010]超级钢琴

    [题解][P2048 NOI2010]超级钢琴 一道非常套路的题目.是堆的套路题. 考虑前缀和,我们要是确定了左端点,就只需要在右端区间查询最大的那个加进来就好了.\(sum_j-sum_{i-1}​ ...

  9. ajax实时获取下拉数据

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ajax ...

  10. Java基础教程:面向对象编程[2]

    Java基础教程:面向对象编程[2] 内容大纲 访问修饰符 四种访问修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default ...