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.

Before I get started, I should write down what I know for now, its a singly-linked-list related problem, and its already been sorted, that mean every duplicate numbers is next to each other. note that it requires to remove all the duplicates, seems like an easy job.

ListNode *deleteDuplicates(ListNode *head) {
ListNode* current, *prev;
ListNode base = ListNode(-10);
base.next = head;
current = &base; prev = NULL;
while (current && current->next){
if (current->val == current->next->val){
while (current->next && current->val == current->next->val){
current = current->next;
}
prev->next = current->next;
current = current->next; }else {
prev = current;
current = current->next;
} }
return base.next;
}

By inserting a dummy node with some invalid number(I choose -1 at first, but a test case uses -1 as 1st val as well...wtf) before head is a good trick, it'll allow you to delete head of current list. anyway, operating linked list in c++ is much convenient then c. In c, you can't change list structure by not involving any double pointer.

[LeetCode] Remove Duplicates from Sorted List II的更多相关文章

  1. Leetcode: Remove Duplicates from Sorted List II 解题报告

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

  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 II (C++)

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

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  7. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  8. [leetcode]Remove Duplicates from Sorted List II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

  9. LeetCode::Remove Duplicates from Sorted List II [具体分析]

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

随机推荐

  1. java socket client

    用tornado做了个socket server.无奈联调的人员对接不上. 于是撸出了以下demo import java.io.*; import java.net.*; public class ...

  2. linux 终端全局代理设置

    http://www.webupd8.org/2010/10/how-to-set-proxy-for-terminal-quick.html 即 export http_proxy='http:// ...

  3. KVC 与 KVO 理解

    KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value Coding (KVC) KVC,即是指 NSKeyValueCoding,一 ...

  4. PHP程序员如何突破成长瓶颈(php开发三到四年)

    看了这篇博文,我正好处于这个阶段,也有心要突破自己,呵呵! 作为Web开发中应用最广泛的语言之一,PHP有着大量的粉丝,那么你是一名优秀的程序员吗?在进行自我修炼的同时,你是否想过面对各种各样的问题, ...

  5. 【云计算】docker三剑客如何支持分布式部署?

    This blog will explain how to create multi-container application deployed on multiple hosts using Do ...

  6. Python模块常用的几种安装方式

    Python模块安装方法 一.方法1: 单文件模块直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块文件夹,执行:pytho ...

  7. ios8 新增的 showViewController 和 showDetailViewController

    1.showViewController 先看看说明: You use this method to decouple the need to display a view controller fr ...

  8. MongoDB 副本集管理(不定时更新)

    简介: 前面介绍完了副本集的搭建.用户的管理.参数和日常操作的说明,那副本集搭建好该如何管理呢?现在来说明下副本集的日常查看和管理. 说明: 1)查看命令行参数:db.serverCmdLineOpt ...

  9. Python~if,while,for~顺序,判断,循环

    if A: for -in : while x: if A:elif:else:       不能直接用int进行迭代,而必须加个range.     range(len(L))     int ob ...

  10. Python批量修改文件名-后缀

    LyncLynn用途: 批量修改文件格式,文件名后缀. #Version: V1.0 #Author:lynclynn #Description:Change the filename #Create ...