题目:

删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素每个元素只留下一个。

 

您在真实的面试中是否遇到过这个题?

样例

给出1->1->2->null,返回 1->2->null

给出1->1->2->3->3->null,返回 1->2->3->null

解题:

Java程序

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
ListNode p = new ListNode(0);
p.next = head;
if(head==null)
return null;
while(head.next!=null){
if(head.val==head.next.val){
head.next = head.next.next;
}else
head = head.next;
} return p.next;
}
}

总耗时: 2604 ms

这个是两个比较,相同就删除一个直接删除,指针变换的比较多

可以向上面一个删除有序数组中相同的元素那样进行

head指向开始节点,current向前走,知道current.val!=head.val时候,head的指针指向current节点

Java程序:

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
ListNode p = new ListNode(0);
p.next = head;
ListNode current = new ListNode(0);
current.next = head;
current = current.next;
if(head==null)
return null;
while(head!=null){
while(current!=null && head.val==current.val){
current = current.next;
}
head.next = current;
head = head.next;
}
return p.next;
}
}

总耗时: 1808 ms

Python程序:

"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: A ListNode
@return: A ListNode
"""
def deleteDuplicates(self, head):
# write your code here
p = ListNode(0)
p.next = head
if head==None:
return None
while head.next!=None:
if head.val==head.next.val:
head.next = head.next.next;
else:
head=head.next
return p.next

总耗时: 319 ms

上面的第二个方法

Python程序:

"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: A ListNode
@return: A ListNode
"""
def deleteDuplicates(self, head):
# write your code here
if head==None:
return None
p = ListNode(0)
p.next = head
cur = ListNode(0)
cur.next = head
cur = cur.next
while head!=None:
while cur!=None and cur.val==head.val:
cur = cur.next
head.next = cur
head = head.next
return p.next

总耗时: 471 ms

lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素的更多相关文章

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

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

  2. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

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

  3. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  4. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

    一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...

  5. [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  6. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  7. LeetCode Remove Duplicates from Sorted List 删除有序链表中的重复结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  8. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  9. leetcode-83.删除排序链表中的重复元素

    leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1- ...

随机推荐

  1. 读取XML

    public sealed class ConfigManger { public XDocument XmlDocs { set; get; } string path = @"{0}\C ...

  2. 支持IE6的树形节结构TreeTable实际应用案例

    <script src="jquery.js" type="text/javascript"></script> <script ...

  3. php 文件上传一例简单代码

    1.程序文件 <?php //判断临时文件存放路径是否包含用户上传的文件 if(is_uploaded_file($_FILES["uploadfile"]["tm ...

  4. Mysql主从同步(复制)

    目录: mysql主从同步定义      主从同步机制 配置主从同步      配置主服务器      配置从服务器 使用主从同步来备份      使用mysqldump来备份      备份原始文件 ...

  5. WPF中的Style

    一.Style基础知识 构成Style最重要的两种元素是Setter和Trigger Setter类帮助我们设置控件的静态外观风格 Trigger类帮助我们设置控件的行为风格 Setter类的Prop ...

  6. NGUI3.5系列教程之 一些小功能的实现

    (一)可拖动窗体的实现: 1:添加一个Sprite为鼠标点击区域,改名为:DragSprite 2:给DragSprite添加Collider 3:给DragSprite添加Drag Object , ...

  7. Android存储机制之Preference

    Preference提供了一种轻量级的数据存取方法,主要是数据比较少的配置信息.它以键值对的方式将数据保存在一个XML配置文件中. 使用Preference方式来存取数据,用到了SharedPrefe ...

  8. PHP获取mysql数据表的字段名称和详细信息的方法

    首先我们需要了解下查询MySQL数据库/表相关信息的SQL语句: 代码如下: SHOW DATABASES                                //列出 MySQL Serv ...

  9. cocos2dx中创建标签CCLabel的三种方法及特点

    创建标签的三种方式:1.CCLabelTTF     (True Type Font,又叫本地字体)这是最简单,也是最常用的方式,不依赖于资源文件,也不依赖于某个系统,所指定的字体如果系统没有,则会提 ...

  10. JPA学习---第八节:使用JPQL语句进行查询

    1.JPQL 语句查询,代码如下: @Test public void query(){ EntityManagerFactory factory = Persistence.createEntity ...