给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字。
例如:
给定 1->2->3->3->4->4->5 ,则返回 1->2->5
给定 1->1->1->2->3 ,则返回 2->3
详见:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/

Java实现:

递归实现:

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

非递归实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode first=new ListNode(-1);
first.next=head;
ListNode p=head;
ListNode last=first;
while(p!=null&&p.next!=null){
if(p.val==p.next.val){
int val=p.val;
while(p!=null&&p.val==val){
p=p.next;
}
last.next=p;
}else{
last=p;
p=p.next;
}
}
return first.next;
}
}

082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II的更多相关文章

  1. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  2. 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项

    给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...

  3. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  4. [LeetCode]题解(python):082 - Remove Duplicates from Sorted List II

    题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Given a sorted linked list ...

  5. Java for LeetCode 082 Remove Duplicates from Sorted List II

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

  6. [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% ...

  7. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

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

  8. 2016.6.17——Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array 本题收获: 1.“删除”数组中元素 2.数组输出 题目: Given a sorted array, remove the du ...

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

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

随机推荐

  1. RobotFramework教程使用笔记——时间控件的相关操作

    在web测试过程中,我们可能会遇到时间控件,有的是支持直接输入的,有的为了保证输入时间格式的一致性是只支持点击选择的,那么这个时候如何用robotframework来操作呢? 看下面这个例子: 这个是 ...

  2. laravel基础课程---1、laravel安装及基础介绍(laravel如何安装)

    laravel基础课程---1.laravel安装及基础介绍(laravel如何安装) 一.总结 一句话总结: [修改composer镜像地址].[明确laravel的安装要求].[安装指定版本的la ...

  3. python 特殊方法之new

    object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static metho ...

  4. (转)vim 访问系统剪贴板

    原文出处:http://vim.wikia.com/wiki/Accessing_the_system_clipboard Please review this tip: This tip was i ...

  5. BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法

    BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1a ...

  6. Battle Ships(复习泛化物品**)

    传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which i ...

  7. 如何使用 Jmeter 发送 Json 请求

    公司最近有一个项目,需要持续发送大量的 Json 请求到服务器,从而测试服务器可靠性. 我就发送 Json 请求部分发布这个博客. 一般来说, Json 请求的数据都保存到 CSV 文件中,然后使用 ...

  8. linux sdk配置

    http://blog.csdn.net/wudj810818/article/details/51251408,环境变量配置不要用 SDK环境变量: export PATH=$PATH:/opt/a ...

  9. py-day2-sys模块、os模块、运算符、列表、字典

    一.sys 模块 import sys print (sys.path)#打印环境变量 print(sys.aegv) #打印脚本的名字相对路径 print(sys.aegv)1 2 3 4 prin ...

  10. bzoj2959

    lct+并查集 联赛之后忘了很多东西 复习一下 这并不是一棵树,所以我们不能直接上lct 但是把双联通分量缩了以后就是一棵树了 怎么缩呢 就是把splay拆了合并到一个点上 连通性和双联通分量拿两个并 ...