Remove Duplicates from Sorted List II

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 1:

使用一个del标记来删除最后一个重复的字元。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
} // record the head.
ListNode dummy = new ListNode(0);
dummy.next = head; ListNode cur = dummy; // to delete the last node in the list of duplications.
boolean del = false; while (cur != null) {
if (cur.next != null
&& cur.next.next != null
&& cur.next.val == cur.next.next.val) {
cur.next = cur.next.next;
del = true;
} else {
if (del) {
cur.next = cur.next.next;
del = false;
} else {
cur = cur.next;
}
}
} return dummy.next;
}
}

SOLUTION 2:

使用一个pre, 一个cur来扫描,遇到重复的时候,使用for循环用cur跳过所有重复的元素。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
} ListNode dummy = new ListNode(0);
dummy.next = head; ListNode pre = dummy;
ListNode cur = pre.next; while (cur != null && cur.next != null) {
if (cur.val == cur.next.val) {
while (cur != null && cur.val == pre.next.val) {
cur = cur.next;
} // delete all the duplication.
pre.next = cur;
} else {
cur = cur.next;
pre = pre.next;
}
} return dummy.next;
}
}

Leetcode: Remove Duplicates from Sorted List II 解题报告的更多相关文章

  1. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告

    明日深圳行,心情紧张,写博文压压惊 囧 ------------------------------------- 原题地址: https://oj.leetcode.com/problems/rem ...

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

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

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

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

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  7. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  8. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  9. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

随机推荐

  1. webDriver.Close() 和 webDriver.Quit() 、webDriver.Dispose() 的区别

    1. webDriver.Close() - Close the browser window that the driver has focus of //关闭当前焦点所在的窗口2. webDriv ...

  2. libev与libevent区别

    摘自stackflow的回答,主要从架构上说明了二者的区别: As for design philosophy, libev was created to improve on some of the ...

  3. IIS状态监测(如果状态错误则重启IIS)

    步骤: 1:建立健康监测文件.文件内容随意,这里以healthcheck.aspx命名,内容是<span>hellow word</span> 2:利用vbs语言执行IIS重启 ...

  4. python对文件操作

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  5. spring 使用注解注入 list 或 map

    1.定义一个接口或抽象类AClass 2.定义两个类实现或继承AClass,(BClass,MClass) 3.在第三个类XClass 中注入List 形如: @Autowired private L ...

  6. 【struts2】继承ActionSupport类

    在Struts2中,Action可以不实现任何特殊的接口或者继承特殊的类,仅仅是一个POJO(Plain Old Java Object,简单的Java对象)就可以:也可以实现Xwork2中的Acti ...

  7. Easyui中 messager出现的位置

    $.messager.alert 弹出框的位置随页面的长度越大越靠下. $.messager.alert('消息','只能对单个客户进行清款!','info'); 弹出的位置 太靠下方.修改为: $. ...

  8. CTreeCtrl鼠标双击响应函数中怎么知道双击的是哪个子项?

    原帖链接: http://bbs.csdn.net/topics/310185501 楼主: CTreeCtrl鼠标双击响应函数中怎么知道双击的是哪个子项? 6楼: CPoint pt;GetCurs ...

  9. github 仓库共享上传权限

    https://blog.csdn.net/qq_33210042/article/details/79717497 打开仓库 -> Settings -> Collaborators 然 ...

  10. databus编译:Could not resolve all dependencies for configuration ':databus2-relay:databus2-event-producer-mock:compile

    FAILURE: Build failed with an exception. * What went wrong: Could not resolve all dependencies for c ...