lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素
题目:
给定一个排序链表,删除所有重复的元素每个元素只留下一个。
您在真实的面试中是否遇到过这个题?
给出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 删除排序链表中的重复元素的更多相关文章
- 【LeetCode】Remove Duplicates from Sorted List(删除排序链表中的重复元素)
这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->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 ...
- lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 ...
- LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素
一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...
- [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 ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- LeetCode Remove Duplicates from Sorted List 删除有序链表中的重复结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- leetcode-83.删除排序链表中的重复元素
leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 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 ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- jquery实现ajax,返回json数据
jquery实现ajax可以调用几种方法 我经常用的是$get(url,data,callback,type)方法 其中url是异步请求的页面(可以是.ashx文件),data是参数,callback ...
- python:执行一个命令行N次
经常希望可以执行一个命令行N次...windows下没有现成的工具(有?推荐给我!) 用python写一个... #!/usr/bin/evn python #coding: utf-8 " ...
- django post报403问题
第一个问题是: 我使用jquery的ajax向后台传值, 当使用GET方法时没问题 $.ajax({ type:"GET" url: data: success: }) 但是由于基 ...
- 1. opencv的初体验
http://guoming.me/opencv-config 这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...
- LAMP开发之环境搭建(2014.12.7在ubuntu下)
Ubuntu下搭建LAMP环境 前言:学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我搭建的是LAMP环境,即Linux.Apache ...
- .NET开发Windows Service程序 - Topshelf
在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...
- 定位表的数据块并且dump出来
SQL> select * from city; ID NAME ---------- ---------- 7 Chicago 6 Jers ...
- GGS: Sybase to Oracle
Step 1: Start the GGSCI on Source and Target Source Target Oracle GoldenGate Command Interpreter for ...