题目

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. MySQL入门很简单: 14MySQL日志

    二进制日志: 以二进制文件的形式记录了数据库中的操作,但不记录查询语句 错误日志: 记录MySQL服务器的启动,关闭和运行错误等信息 通用查询日志: 记录用户登录和记录查询的信息 慢查询日志: 记录执 ...

  2. Socket的基本使用步骤

    Socket的基本使用步骤 一.使用Socket,首先需要导入这几个系统头文件 #import <sys/socket.h> #import <netinet/in.h> #i ...

  3. 如何在win10中安装ArcGIS10.2

    在win10中安装ArcGIS10.2,完美兼容,下面将自己在win10界面下的安装方法给大家分享一下. 工具/原料   win10环境 ArcGIS10.2安装包, 安装包地址链接: 链接: htt ...

  4. 2017.10.12 Java的计数器的开发

    //我们用一个合成的applet/application来简单显示出一个计数器的结果/** * Created by qichunlin on 2017/10/12. */ /*简单的计数器*/ im ...

  5. Spring boot 项目导出可执行jar

    配置文件中添加插件 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  6. device not ready cuda

    问题描述: CUDA: 使用cudaEventElapsedTime时返回device not ready error 强调下我是用谷歌大神搜索到的结构哦! http://stackoverflow. ...

  7. pooling

    转自:http://www.gageet.com/2014/09182.php 本文部分参考了:http://www.zhihu.com/question/23437871 卷积层是对图像的一个邻域进 ...

  8. Java Web入门经典扫描版

    全书共分4篇19章,其中,第一篇为“起步篇”,主要包括开启JavaWeb之门.不可不知的客户端应用技术.驾驭JavaWeb开发环境.JavaWeb开发必修课之JSP语法等内容:第二篇为“核心篇”,主要 ...

  9. 解决nsis error!cant initialize plug-ins directory.please try again later

    情况1: 调用SectionEnd会释放掉dll初始化标记,所有Section都必须放在函数的最下面. 情况2: 有可能是栈里的数据错乱,特别注意的是,使用BgWorker.dll获取多线程能力的时候 ...

  10. top小火箭

    // my.js function $(id){return document.getElementById(id)};function show(obj){obj.style.display = & ...