/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class RemoveDuplicates2 { /**
* 移除链表中所有重复的元素
*
* @param head
* @return
*/
public Node remove (Node head) {
Node dummy = new Node();
dummy.next = head;
Node cur = head;
Node pre = dummy;
boolean duplicated = false;
while (cur != null && cur.next != null) {
if (cur.value == cur.next.value) {
cur.next = cur.next.next;
duplicated = true;
} else if (duplicated) {
// 如果出现重复则移除重复的元素
pre.next = cur.next;
cur = pre.next;
duplicated = false;
} else {
pre = cur;
cur = cur.next;
}
}
if (duplicated) {
pre.next = cur.next;
}
head = dummy.next;
return head;
} private static class Node implements Comparable<Node>{
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
} @Override
public int compareTo(Node o) {
return this.value - o.value;
}
} private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
System.out.println();
} public Node createList (int[] arr) {
if (arr.length == 0) {
return null;
}
Node head = new Node();
head.value = arr[0];
Node pointer = head;
for (int i = 1; i < arr.length; i++) {
Node node = new Node();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
} public static void main(String[] args) {
RemoveDuplicates2 removeDuplicates2 = new RemoveDuplicates2(); int[] arr = new int[]{1,1,2};
int[] arr1 = new int[]{1,1,2,3,3};
int[] arr2 = new int[]{1,1,2,3,3,3,4,4,5};
print(removeDuplicates2.remove(removeDuplicates2.createList(arr)));
print(removeDuplicates2.remove(removeDuplicates2.createList(arr1)));
print(removeDuplicates2.remove(removeDuplicates2.createList(arr2)));
} }

leetcode — remove-duplicates-from-sorted-list-ii的更多相关文章

  1. Leetcode: Remove Duplicates from Sorted List II 解题报告

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

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

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

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

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

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

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

  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 @ Python

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

  8. [LeetCode] Remove Duplicates from Sorted List II

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

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

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

  10. LeetCode::Remove Duplicates from Sorted List II [具体分析]

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

随机推荐

  1. Vue.js的安装及简单使用

    一.Vue简介 二.Vue.js的安装 2.1.npm安装 2.1.1.node.js介绍及安装 简介: 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基 ...

  2. React事件绑定几种方法测试

    前提 es6写法的类方法默认没有绑定this,不手动绑定this值为undefined. 因此讨论以下几种绑定方式. 一.构造函数constructor中用bind绑定 class App exten ...

  3. Java之hashCode的作用和equals方法的重构规则

    这个是博主对hashcode的初步理解,以后加深了会再来更新: 1.hashcode是什么? hashcode是对象的散列码,不同的对象几乎不一样,说几乎是因为还是可以一样的. 特点:每一个对象都有h ...

  4. vbs编写一个函数,将1001到1050(50串数字)读入test.txt文件。每串数字占一行,不是覆盖。

    Option Explicit---------------------------------------------------------开头 dim fas,objfso,printstr,o ...

  5. python3脚本打开摄像头

    openCamera 脚本地址:https://github.com/Mrlshadows/openCamera Mac OS 安装 OpenCV Python 环境为 python3 终端执行如下指 ...

  6. PageHelper分页插件及通用分页js

     分页概述 1.物理分页 物理分页依赖的是某一物理实体,这个物理实体就是数据库,比如MySQL数据库提供了limit关键字,程序员只需要编写带有limit关键字的SQL语句,数据库返回的就是分页结果. ...

  7. 深入分析volatile的实现原理

    synchronized是一个重量级的锁,虽然JVM对它做了很多优化,而下面介绍的volatile则是轻量级的synchronized.如果一个变量使用volatile,则它比使用synchroniz ...

  8. 音视频编解码技术(二):AAC 音频编码技术

    一.AAC编码概述 AAC是高级音频编码(Advanced Audio Coding)的缩写,出现于1997年,最初是基于MPEG-2的音频编码技术,目的是取代MP3格式.2000年,MPEG-4标准 ...

  9. [Swift]LeetCode519. 随机翻转矩阵 | Random Flip Matrix

    You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all ...

  10. [Swift]LeetCode898. 子数组按位或操作 | Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...