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. ML之监督学习算法之分类算法一 ———— k-近邻算法(最邻近算法)

    一.概述 最近邻规则分类(K-Nearest Neighbor)KNN算法 由Cover 和Hart在1968年提出了最初的邻近算法, 这是一个分类(classification)算法 输入基于实例的 ...

  2. 【SqlServer】在SqlServer中把数据导入导出为Excel文件

    这里笔者介绍利用SqlServer数据库操作EXECEL文件. 1.将Excel表中的数据导入为SqlServer数据库 把Excel表中的数据导入为SqlServer数据库中的数据. 新建一个Exc ...

  3. scala+hadoop+spark环境搭建

    一.JDK环境构建 在指定用户的根目录下编辑.bashrc文件,添加如下部分: # .bashrc # Source global definitions if [ -f /etc/bashrc ]; ...

  4. Oracle 12C -- 设置CDB启动后,PDBs自动启动

    CDB重启后,PDBs默认是处于mounted状态 SQL> select name,open_mode from v$pdbs; NAME OPEN_MODE ---------------- ...

  5. 第1章 Python基础-Python介绍&循环语句 练习题&作业

    1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型? 高级语言分为编译型与解释型两种,分别从执行速度.开发效率.跨平台性三个方面说它们的区别. 编译型语言因为执行的 ...

  6. ICDAR2015 数据处理及训练

    训练数据处理: 天池ICPR2018和MSRA_TD500两个数据集: 1)天池ICPR的数据集为网络图像,都是一些淘宝商家上传到淘宝的一些商品介绍图像,其标签方式参考了ICDAR2015的数据标签格 ...

  7. python执行selenium报错

    python + unittest + HTMLTestRunner 执行suite用例报错 Traceback (most recent call last): File "C:/ws/S ...

  8. Windows 平台下Myeclipse 10 中SVN 插件使用教程(TortoiseSVN)

    1.  TortoiseSVN 简介 版本控制是管理信息修改的艺术,它一直是程序员最重要的工具,程序员经常会花时间作出小的修改,然后又在某一天取消了这些修改,想象一下一个开发者并行工作的团队 - 或许 ...

  9. AR模型与数据平稳性之间的关系

    作者:桂. 时间:2017-12-19  21:39:08 链接:http://www.cnblogs.com/xingshansi/p/8068021.html 前言 前几天碰到一个序列分析的问题, ...

  10. Ambari修改主页面方法

    [root@hdp159 ambari-web]# brunch watch --server Oct :: - info: application started on http://localho ...