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

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

这个题目跟[LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List很类似,但是有可能要把head都去掉,所以还是要加上dummy node,并且使得dummy.next = head, 同时设置pre = dummy instead of None, 另外while loop判断条件为head and head.next, 最后返回dummy.next.

Code

class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def removeDup2(self, head):
dummy = ListNode(0)
dummy.next = head
pre = dummy
while (pre.next and pre.next.next):
if pre.next.val == pre.next.next.val:
val = pre.next.val
while (pre.next and pre.next.val == val): # 把重复的点都删掉
pre.next = pre.next.next
else:
pre = pre.next
return dummy.next

2)

class Solution:
def removeDup2(self, head):
dummy = ListNode(0)
dummy.next = head
pre = dummy
while head and head.next:
if head.val == head.next.val:
val = head.val
while head and head.val == val:
pre = head.next
head.next = None
head = pre.next
else:
pre = head
head = head.next
return dummy.next

[LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List的更多相关文章

  1. [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 ...

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

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

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

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

  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 82.Remove Duplicates from Sorted List II (删除排序链表的反复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

    第一题:遍历链表,遇到重复节点就连接到下一个. public ListNode deleteDuplicates(ListNode head) { if (head==null||head.next= ...

  7. 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题要求 ...

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

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

  9. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

随机推荐

  1. A complete example using RAISE_APPLICATION_ERROR : raise_application_error

    from:http://www.java2s.com/Tutorial/Oracle/0480__PL-SQL-Programming/AcompleteexampleusingRAISEAPPLIC ...

  2. Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续一)

    通过前一节得出地址可能的构建规律,如下: https://s.taobao.com/search?data-key=s&data-value=44&ajax=true&_ksT ...

  3. oracle中获取执行计划

    1. 预估执行计划 - Explain PlanExplain plan以SQL语句作为输入,得到这条SQL语句的执行计划,并将执行计划输出存储到计划表中. 首先,在你要执行的SQL语句前加expla ...

  4. 浅谈Socket长连+多线程

    缘由 不知各位同仁有没有发现,用简单,无外乎就是都是一个流程 1)监听链接 2)校验链接是否是正常链接 3)保存链接至全局静态字典 4)开启新线程监听监听到的线程报文 5)执行对应命令或者发送对应命令 ...

  5. 演示PostgreSQL的详细安装及配置图解

    右击文件选择以管理员身份运行 2 开始执行程序的安装 3 设置安装目录 4 设置数据的保存目录 5 设置数据库管理员密码,请牢记此密码. 6 设置端口号,选择默认的端口号即可 7 根据自己选择设置地区 ...

  6. 虚拟机怎么设置u盘启动

    方法/步骤   1 运行你安装的虚拟机 步骤阅读 2 点击绿色的按钮,把你的虚拟机下面的系统启动. 步骤阅读 3 让你的虚拟系统处于可以按“Ctrl+Alt+Insert”重启的界面.比如我让虚拟系统 ...

  7. c++虚函数[转]

    C++ 虚函数表解析 陈皓 http://blog.csdn.net/haoel 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父 ...

  8. 【BZOJ1096】[ZJOI2007]仓库建设 斜率优化

    [BZOJ1096][ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司 ...

  9. iOS UITextField输入后隐藏键盘

    1.首先在Interface Builder中选择TextFields,然后在Text Field Attributes中找到Text Input Traits,选择Return Key为done. ...

  10. 批量远程执行linux服务器程序--基于pxpect(多进程、记日志版)

    #!/usr/bin/python '''Created on 2015-06-09@author: Administrator''' import pexpect import os,sys fro ...