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.

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(!head) return head; ListNode *previous = NULL;
ListNode *current = head;
bool repeat = false;
while(current)
{
while(current->next && current->val == current->next->val)
{
current = current->next;
repeat = true;
}
if(repeat && previous!= NULL)
{
previous->next = current->next;
}
else if(!repeat)
{
if(!previous) head = current;
previous = current;
} current = current->next;
repeat = false;
}
if(!previous) return previous;
return head;
}
};

82. Remove Duplicates from Sorted List II (List)的更多相关文章

  1. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  2. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

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

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

  5. LeetCode OJ 82. Remove Duplicates from Sorted List II

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

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

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

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

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

  8. [LeetCode#82]Remove Duplicates from Sorted Array II

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

  9. 82. Remove Duplicates from Sorted List II

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

  10. leetcode 82. Remove Duplicates from Sorted List II

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

随机推荐

  1. 记录一些WPF常用样式方便以后复用(转)

    TextBox文本框 <Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{ ...

  2. 基于C#的UDP协议的异步实现

    一.摘要 总结UDP传输协议的异步实现. 二.实验平台 visual studio 2010 三.实验实例 服务器端代码: using System; using System.Collections ...

  3. [LeetCode系列]最大容器问题

    给定n个数: a1, a2, ... , an. 代表着(i, ai)个点, 连接这些点与对应的(i, 0), 我们可以得到n条线. 请在这n条线中找出2条, 使得这两条线和x轴构成的容器能够容纳最多 ...

  4. ubuntu 12.04 下 Vim 插件 YouCompleteMe 的安装

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4137402.html 1.需要保证vim的版本大于7.3.584,否则的话需要更新vim 可 ...

  5. i.e 和e.g 的区别

    i.e 和e.g 的区别 两者都是拉丁文缩写 i.e是id est的缩写,意思是that is. e.g是exempli gration的缩写,意思是for example;

  6. LNMP.ORG 安装LNMP

    参考:http://lnmp.org/install.html cd /usr/local/src wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.ta ...

  7. Linux下定时执行任务(crontab命令)

    1.循环执行的计划任务 linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作用的,所以crond才符合我 ...

  8. erlang的websocket例子

    创建工程 rebar-creator create-app websocket_demo 文件列表 route_helper.erl -module(route_helper). -export([g ...

  9. 使用FileZilla连接Linux

      FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能.可控性.有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP ...

  10. TimesTen学习(三)安装、连接、远程连接TimesTen数据库

    TimesTen学习(三)远程连接TimesTen数据库 <TimesTen学习(一)安装篇>:http://blog.itpub.net/23135684/viewspace-71774 ...