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. Selenium WebDriver使用IE浏览器 属性设置

    System.setProperty("webdriver.ie.driver", "D:\\developsoft\\Selenium_Test\\IEDriverSe ...

  2. NFS客户端、服务器协商读写粒度(rsize、wsize)流程 【转】

    rsize和wsize决定了网络文件系统(NFS)一次网络交互所能够读写的数据块的大小,rsize和wsize的大小对网络文件系统(NFS)的性能有重要影响.rsize和wsize的大小是在用户配置的 ...

  3. ROS学习(九)—— rqt_console 和 roslaunch

    一.rqt_console 和rqt_logger_level 1.作用: rqt_console依据ROS编译日志,输出节点信息 rqt_logger_level可以改变节点的警告出差的警告等级 2 ...

  4. 配置Hadoop1.2.1

    1.从Apache官网上下载1.2.1,地址:http://apache.dataguru.cn/hadoop/common/2.拷贝文件到虚拟机下(vm9下直接拖拽就可以)3.到Hadoop的目录下 ...

  5. 【Algorithm】希尔排序

    一. 算法描述 希尔排序:将无序数组分割为若干个子序列,子序列不是逐段分割的,而是相隔特定的增量的子序列,对各个子序列进行插入排序:然后再选择一个更小的增量,再将数组分割为多个子序列进行排序..... ...

  6. openkm预览功能报错:flexpaper License key not accepted(no key passed to viewer)

    openkm:6.3.4 使用google浏览器打开,想预览文件,但是pdf.word和图片都不能显示.只是显示空白. 换成IE后,再次尝试,发现了报错信息: 解决方案: 1- Stop openkm ...

  7. linux中WDCP的日志彻底删除技巧

    apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...

  8. Unix网络编程 之 基本套接字调用(一)

    Unix/Linux支持伯克利风格的套接字编程,它同一时候支持面向连接和面向无连接类型的套接字. 套接字最经常使用的一些系统调用: socket() bind() connect() listen() ...

  9. jenkins启动appium服务

    想在jenkins中,自动定时启动appium服务,shell命令已准备如下: BUILD_ID=dontKillMe echo "" > appium.log nohup ...

  10. eclipse 运行 emulator时,PANIC:Could not open emulator 的解决办法

    使用eclipse启动emulator的时候,出现PANIC:Could not open emulator,模拟器无法正常的运行. 经过搜索得知,因为我的SDK的环境变量出问题,需要重新配置下环境变 ...