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. Hyper-V 手动导入虚机配置实例(转载)

    原文转载:http://blog.51cto.com/bobzy/980241 Hyper-V提供了很方便的虚机导入和导出功能.平时假如我们想导出虚机,先选中虚机,然后鼠标右键在出现菜单列表中选中“导 ...

  2. jsp_include

    jsp__include指令先包含后编译 include 行为 先编译后包含 <jsp:include page="head.jsp"></jsp:include ...

  3. webstorm配置scss的小结

    1)安装ruby 2)安装sass 3)配置webstorm 打开webstrom ->file->setting->Tools->file watcher 添加scss pr ...

  4. oracle的时间

    今天发现之前从oracle导出的数据时间格式很奇怪: "ACTIVETIME","ACTIVITYID" "2018-08-10 06:08:43&q ...

  5. PPP PDP 及GPRS

    1.相关概念: PDP:Packet Data Protocol 分组数据协议 PLMN:Public Land Mobile Network,公共陆地移动网络 APN:Access Point Na ...

  6. windows7下怎样安装whl文件(python)

    本文转载自:http://blog.csdn.net/fhl812432059/article/details/51745226 windows7 python2.7 1.用管理员方式打开cmd 2. ...

  7. 用活Firewalld防火墙之direct

    原文地址:http://www.excelib.com/article/294/show 学生在前面已经给大家介绍过了Firewalld中direct的作用,使用他可以直接使用iptables.ip6 ...

  8. mysql索引之四:复合索引之最左前缀原理,索引选择性,索引优化策略之前缀索引

    高效使用索引的首要条件是知道什么样的查询会使用到索引,这个问题和B+Tree中的“最左前缀原理”有关,下面通过例子说明最左前缀原理. 一.最左前缀索引 这里先说一下联合索引的概念.MySQL中的索引可 ...

  9. 操作系统-百科: UNIX

    ylbtech-操作系统-百科: UNIX UNIX操作系统(尤尼斯),是一个强大的多用户.多任务操作系统,支持多种处理器架构,按照操作系统的分类,属于分时操作系统,最早由KenThompson.De ...

  10. 建立SIP通话

    建立SIP: 点击下的出现的页面: 选择submit,只用填写用户名和密码就OK了,secret是密码,填写完以后记得应用 创建完毕以后,使用xlite去连接:xlite的配置:域名是asterisk ...