题目:

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

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

 

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

样例

给出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. 批处理测试局域网网络连通性ping1-255

    for /l %%1 in (1 1 255)do ping /n 1 192.168.1.%%1       ##bat下 运行 for /l %i in (1,1,254) do ping -n ...

  2. c# WinForm 编程总结

    1.清空DataGridView /// <summary> /// 清空DataGridView /// </summary> /// <param name=&quo ...

  3. nginx总结

    kill int 2333  进程号   停止程序 kiil quit 2322  优雅停止服务 kill -HUP 2333  优雅重启 从新读取配置文件 kill -HUP 'cat logs/n ...

  4. Mysql多表查询(两张独立表,一张关系表)

    一.数据库设计 1.三个数据表长这样   其中user表记录用户信息,cat主要记录男女性别,mete表是用户id和性别id的对应关系   2.具体数据如下   二.查询目标 查询出所有性别为“男”的 ...

  5. Configuration python CGI in XAMPP in win-7

    1.After install XAMPP,we need add the path of the Mysql just find the path and add it to your sys-pa ...

  6. setEllipsize(TruncateAt where)

    void android.widget.TextView.setEllipsize(TruncateAt where) public void setEllipsize (TextUtils.Trun ...

  7. Python脚本控制的WebDriver 常用操作 <二十七> 文件下载

    测试用例场景 webdriver允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中. Python脚本 测试用Python代码: # coding=gbk ''' Crea ...

  8. 【nodejs】 文件系统(fs) 之读写文件

    //写入文件 var data = "hello world"; fs.writeFile('c:\\a.txt', data, 'ascii', function(err) { ...

  9. [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等

    [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等 http://blog.csdn.net/yiya1989/article/details/784 ...

  10. html5游戏引擎-Pharse.js学习笔记(一)

    1.前言 前几天随着flappy bird这样的小游戏的火爆,使我这种也曾了解过html5技术的js业余爱好者也开始关注游戏开发.研究过两个个比较成熟的html5游戏引擎,感觉用引擎还是要方便一些.所 ...