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.

题目大意:给定一个有序的单链表,删除所有重复元素,只保留出现一次的。

解题思路:记录前驱节点,如果当前元素是重复的,直接把前驱节点的next指向后一个,循环一遍即可。代码写的还是很纠结。WA了五六次。

    public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode dummy = new ListNode(0);
ListNode ptr = head;
while(head!=null){
ptr=head;
while(ptr.next!=null&&ptr.val==ptr.next.val){
ptr=ptr.next;
}
if(head!=ptr){
head=ptr.next;
}else{
break;
}
}
dummy.next=head;
ListNode tmp = head;
while(tmp!=null){
ptr=tmp.next;
if(ptr==null){
break;
}
while(ptr!=null&&ptr.next!=null&&ptr.val==ptr.next.val){
ptr=ptr.next;
}
if(ptr==tmp.next){
tmp=tmp.next;
continue;
}
tmp.next=ptr.next;
}
return dummy.next;
}

Remove Duplicates from Sorted List II ——LeetCode的更多相关文章

  1. Remove Duplicates from Sorted Array II [LeetCode]

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. Remove Duplicates from Sorted Array II ——LeetCode

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  4. Remove Duplicates from Sorted List II [LeetCode]

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

  5. Remove Duplicates from Sorted List II leetcode java

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

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

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

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

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

随机推荐

  1. 16、SQL Server 复制及常见错误处理

    SQL Server 复制 复制是一组技术的组合,可以用此组合对数据和数据库对象进行复制由一个数据库移动到另一个数据库. 复制的英文是Replication,重复的意思,而不是Copy.复制的核心功能 ...

  2. iBatis 的简单入门

    iBATIS一词来源于“internet”和“abatis”的组合,于2010年6月16号被谷歌托管,改名为MyBatis.是一个基于SQL映射支持Java和·NET的持久层框架. ibatis本是a ...

  3. spring事务回滚无法捕捉

    这篇文章讲解了怎么配置才能让spring事务捕捉异常 http://www.360doc.com/content/12/1109/18/6161903_246870991.shtml 需要正确配置sp ...

  4. ASP.NET如何发布更新

    如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...

  5. java中json转xml

    参考:http://heipark.iteye.com/blog/1394844 需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下: xml ...

  6. 使用EMMET中的小坑

    使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...

  7. Web Service属性介绍

    每个 Web Service都需要唯一的命名空间,它可使客户端应用程序区分出可能使用相同方法名称的 Web Service.在 Visual Studio.NET中创建的Web Service的默认命 ...

  8. ValidationContext

    .NET 4 和Silverlight 中可以使用以下方法: ? public static void Validate(this Entity entity) {     // prepare th ...

  9. 外部式css样式,写在单独的一个文件中

    外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<style>标签内)使用<l ...

  10. CSS居中的方法总结

    [水平居中] 行内:text-align:center; 定宽块状:1.left:0 right:0然后用margin: auto外边距填充,水平方向不会发生外边距叠加;  2.绝对定位(父元素定位不 ...