给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字。
例如:
给定 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. 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分

    题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...

  2. c语言之秒数算法

    // 水仙花树:是指一个3位数字,立方和 等于该数本身 // 秒数算法:随便输入一个大于0的数,求出对应的多少小时多少分钟多少秒 #include <stdio.h> / int main ...

  3. [原创]java对word文档的在线打开

    一.材料准备 百度一下:PageOffice,从官网下载PageOffice for Java.压缩包文件: 二. 实现步骤: 1. 打开“集成文件”目录,拷贝“WEB-INF\lib”目录中的pag ...

  4. [CTSC 2018] 混合果汁

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5343 [算法] 对于每组询问 , 首先二分答案 显然 , 最优策略为优先选择价格低的 ...

  5. vue 表单验证省市县三联动

    <el-col :span="24"> <el-form-item label="所在地区" prop="region" ...

  6. Quicklz压缩算法

    以前对压缩算法一无所知,只是知道哈弗曼编码能做这种事情,但是感觉这样的方法奇慢无比.昨天下午看了下号称世界上最快的压缩算法Quicklz,对压缩的基本思路有了一定的了解.一般的压缩程序的要求读入文件之 ...

  7. HDU2844(多重部分和)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. HDU1083(最大匹配)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. myeclipse配置

    windows->preference->MyEclipse->servers->tomcat 选项下 Tomcat 6.x 点 enable 设置tomcat directo ...

  10. 如何给容器服务的Docker增加数据盘

    如何给容器服务的Docker增加数据盘 摘要: 我们知道Docker的数据是通过联合文件系统的方式存储到磁盘上,当需要在机器上运行的容器或者镜像的数量不断增加时,有可能磁盘的大小不再满足需求,这个时候 ...