Question

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.

Solution

Note here we need to consider head of the list. For example, for input 1 -> 1, we need to return null.

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
while (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val)
head = head.next;
if (head.next != null)
head = head.next;
else
return null;
}
ListNode current = head, next1, next2;
while (current != null && current.next != null && current.next.next != null) {
next1 = current.next;
next2 = current.next.next;
if (next1.val != next2.val) {
current = current.next;
} else {
while (next2 != null && next1.val == next2.val)
next2 = next2.next;
current.next = next2;
}
}
return head;
}
}

Remove Duplicates from Sorted List II 解答的更多相关文章

  1. Remove Duplicates from Sorted List II 解答(有个比較特殊的case leetcode OJ没有覆盖)

    昨天被考了一道数据结构题,当时的实现比較一般.回来翻看leetcode,果然是上面的题.遂解之. accept之后翻看discuss别人的解法.发现非常多能够accept的代码都过不了我设计的一个ca ...

  2. Remove Duplicates from Sorted List II

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

  3. 【leetcode】Remove Duplicates from Sorted Array II

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

  4. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  5. 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...

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

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

  7. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  8. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

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

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

随机推荐

  1. nyoj 234 吃土豆

    描述 Bean-eating * grid. Now you want to eat the beans and collect the qualities, but everyone must ob ...

  2. WPF动画

    System.Windows.Media.Animation 这个命名空间中,包含了三种动画类:线性插值动画类(17个).关键帧动画(22个).路径动画(3个). 线性插值动画类(17个)如下: By ...

  3. servlet获得完整路径

    request.getQueryString() request.getParameterMap() request.getParameterNames() 在servlet中GET请求可以通过Htt ...

  4. ASP.NET开发学习视频教程大全(共800集)

    ASP.NET是微软.NET平台的支柱之一,被广泛应用在WEB等互联网开发领域,因此它的强大性和适应性,可以使它运行在Web应用软件开发者的几乎全部的平台上.这里整理了最全的ASP.NET开发学习视频 ...

  5. 移动前端开发之 viewport 的深入理解

    移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或响 ...

  6. php使用session来保存用户登录信息

    php使用session来保存用户登录信息 使用session保存页面登录信息 1.数据库连接配置页面:connectvars.php <?php //数据库的位置 define('DB_HOS ...

  7. Git应用于Android项目的入门知识:我的理解

    Git应用于Android项目的基本知识.     常常将git,repo和gerrit三种工具配合起来使用,使Android开发中的部分工作自动化.并适应敏捷项目管理的需要.     repo是Go ...

  8. java序列化ClassNotFoundException

    简单的想从保存的对象中重新解析出对象,用了逆序列化,可是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassL ...

  9. iOS 面试题汇总

    1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承;可以实现多个接口,通过实现 ...

  10. automaticallyAdjustsScrollViewInsets的作用

    简单点说就是automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview ...