题目简述

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,

Given 1->1->2, return 1->2.

Given 1->1->2->3->3, return 1->2->3.

解题思路

# 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 == None:
return head
pre = ListNode(-1)
p = ListNode(-1)
p = pre = head
while p != None:
if p.val == pre.val:
if p.next == None:
pre.next = None
else:
pre.next = p.next
p = pre.next
else:
p = p.next
pre = pre.next
return head

【leetcode】Remove Duplicates from Sorted List的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  4. 【leetcode】 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. 【leetcode】Remove Duplicates from Sorted List (easy)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. 【Leetcode】Remove Duplicates from Sorted List in JAVA

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. 【leetcode】Remove Duplicates from Sorted List II (middle)

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

  8. 【Leetcode】Remove Duplicates from Sorted List II

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

  9. 【LeetCode】Remove Duplicates from Sorted List(删除排序链表中的重复元素)

    这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

随机推荐

  1. vue-router2使用

    条件:紧接前面vue.js开发环境搭建 1.在cmd输入:npm install vue-router,回车,等待,安装对应版本router: 2.在入口文件用 import vueRouter fr ...

  2. coreseek+sphinx+mysql+thinkphp整合

    1.安装coreseek 1.1首先升级或安装系统依赖库 yum install make gcc g++ automake libtool mysql-client libmysqlclient15 ...

  3. C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  4. js作用域

    一.js没有块级作用域 在c,java等语言中花括号里的代码都有自己的作用域,而js花括号没有块级作用域,经常会导致一些困惑,不明所以.例如: console.info(color); if(true ...

  5. jQuery动画slideUp()不正常位移原因

    用jQuery写一个列表.当点击底部按钮时,列表中序号超过6的项目可以向下拉出或者向上收起. 用slideUp(),遇见一个问题.展开列表项会产生不正常位移,如下图所示.动画结束发生位移. 出现这个问 ...

  6. SDC Tcl package of Timequest

    Tcl comand Tcl Commands all_clocks all_inputs all_outputs all_registers create_clock create_generate ...

  7. Maven POM元素继承

    为了减少重复代码的编写,我们需要创建POM的父子结构,然后在POM中申明一些配置供子POM继承,以实现"一处申明,多处使用的"目的.以之前的模块中的结构为基础,在account-a ...

  8. 读取图像,LUT以及计算耗时

    使用LUT(lookup table)检索表的方法,提高color reduce时对像素读取的速度. 实现对Mat对象中数据的读取,并计算color reduce的速度. 方法一:使用Mat的ptr( ...

  9. 生产环境中,数据库升级维护的最佳解决方案flyway

    官网:https://flywaydb.org/ 转载:http://casheen.iteye.com/blog/1749916 1.  引言 想到要管理数据库的版本,是在实际产品中遇到问题后想到的 ...

  10. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...