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.

解题思路:

用指针即可,JAVA实现如下:

    public ListNode deleteDuplicates(ListNode head) {
while (head != null && head.next != null) {
ListNode temp = head.next;
if (temp.val == head.val) {
while (temp.val == head.val) {
temp = temp.next;
if (temp == null)
return null;
}
head = temp;
continue;
} else
break;
}
if (head == null || head.next == null)
return head;
ListNode temp = head.next;
ListNode last = head;
while (temp != null && temp.next != null) {
if (temp.val != temp.next.val) {
last.next = temp;
temp = temp.next;
last=last.next;
continue;
}
int tmp=temp.val;
while(temp.val==tmp){
temp=temp.next;
if (temp == null){
last.next=null;
return head;
}
}
}
last.next=temp;
return head;
}

Java for LeetCode 082 Remove Duplicates from Sorted List II的更多相关文章

  1. Java for LeetCode 080 Remove Duplicates from Sorted Array II

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

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

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

  3. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  4. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

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

  5. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

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

  7. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  8. LeetCode OJ Remove Duplicates from Sorted Array II

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

  9. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

随机推荐

  1. Displaying Tabbed and Stacked Canvas Using Show_View In Oracle Forms

    Displays the indicated canvas at the coordinates specified by the canvas's X Position and Y Position ...

  2. mac下安装好sencha cmd后每次都需要输入source ~/.bash_profile

    解决办法: 在终端输入vim ~/.zshrc加一句 source ~/.bash_profile

  3. http各类攻击及tcpcopy工具

    1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...

  4. 2016.7.12 Table configuration with catalog null, schema public, and table globalpage did not resolve to any tables(疑)

    在eclipse中运行mybatis的generator插件时,出现如下错误提示: Generation Warnings Occured:Table configuration with catal ...

  5. Java架构师与开发者提高效率的10个工具

    Java受到全球百万计开发者的追捧,已经演变为一门出色的编程语言.最终,这门语言随着技术的变化,不断的被改善以迎合变化的市场需求. 无论你是否拥有一家科技公司,软件已经成为几乎每一个企业不可或缺的一部 ...

  6. Codeforces Round #243 (Div. 2)——Sereja and Table

    看这个问题之前,能够先看看这个论文<一类算法复合的方法>,说白了就是分类讨论,可是这个思想非常重要 题目链接 题意: 首先给出联通块的定义:对于相邻(上下和左右)的同样的数字视为一个联通块 ...

  7. HTML5移动开发实战必备知识——本地存储(2)

    了解了一些主要的本地存储使用方法和思想后.我们来系统的介绍一下本地存储. 本地存储分为三大类:localStorage/sessionStorage/本地数据库 localStorage和sessio ...

  8. 使用jquey的css()方法改变样式,

    $("#tip").css("display","none"); $("#tip").css("display ...

  9. 面试题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变?

    /* * 问题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变 * 答: * 使用finalkeyword修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内 ...

  10. ubuntu16.04 Cmake学习二

    本节主要总结编译程序的时候使用了第三方库的情况,以调用开源opencv-2.4.9为例子,具体安装详见http://www.cnblogs.com/xsfmg/p/5900420.html. 工程文件 ...