题目

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.

代码:oj在线测试通过 288 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
if head is None or head.next is None:
return head dummyhead = ListNode(0)
dummyhead.next = head p = dummyhead
while p.next is not None and p.next.next is not None:
tmp = p
while tmp.next.val == tmp.next.next.val:
tmp = tmp.next
if tmp.next.next is None:
break
if tmp == p:
p = p.next
else:
if tmp.next.next is not None:
p.next = tmp.next.next
else:
p.next = tmp.next.next
break
return dummyhead.next

思路

设立虚表头 hummyhead 这样处理Linked List方便一些

p.next始终指向待比较的元素

while循环中再嵌套一个while循环,把重复元素都跳过去。

如果遇到了重复元素:p不动,p.next变化;如果没有遇到重复元素,则p=p.next

Tips: 使用指针之前 最好加一个逻辑判断 指针不为空

leetcode 【 Remove Duplicates from Sorted List II 】 python 实现的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. [LeetCode] Remove Duplicates from Sorted List II

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

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

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

随机推荐

  1. Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?

    打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...

  2. 少年开始学习c#编程,过路的大神请担待!

    这应该真正算开始学习编程, 安装好了 linux, 开通了博客员的博客, 同时今天也需要把sublime安好, 安装MonoDevelop Codeblocks mysql-workbench 配置好 ...

  3. IOS 多线程-NSThread 和线程状态

    @interface HMViewController () - (IBAction)btnClick; @end @implementation HMViewController - (void)v ...

  4. 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?

    因为红黑树需要进行左旋,右旋操作, 而单链表不需要,以下都是单链表与红黑树结构对比.如果元素小于8个,查询成本高,新增成本低如果元素大于8个,查询成本低,新增成本高 https://bbs.csdn. ...

  5. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  6. 轻量级HTTP服务器Nginx(常用配置实例)

    轻量级HTTP服务器Nginx(常用配置实例)   文章来源于南非蚂蚁   Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...

  7. C++STL之set集合容器

    set集合容器 set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构, 在 插入元素时, 它会自动调整二叉树的排列, 把该元素放到适当的位置, 以确保每个子树根节点的键 ...

  8. asp .net core 中间件的简单 使用

    在startup 的cs文件中 2.捕获异常的中间件 可以在浏览器中 显示异常信息 在开发环境的境况下,该中间件会帮你 捕获异常

  9. File类,递归

    File类 File文件和目录路径名的抽象表示形式.即,Java中把文件或者目录(文件夹)都封装成File对象. File类包含     路径    path E:\...     目录 direct ...

  10. Spring 学习之bean的理解

    前言:对于使用Spring框架的开发人员来说,我们主要做的主要有两件事情:①开发Bean;②配置Bean;而Spring帮我们做的就是根据配置文件来创建Bean实例,并调用Bean实例的方法来完成“依 ...