leetcode — remove-duplicates-from-sorted-list-ii
/**
* 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的更多相关文章
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- jquery 点击事件切换样式
$('#FatherName').on('click', '.ClassName', function(e){ $('.ClassName').removeClass('active'); $(thi ...
- android BLE Peripheral 模拟 ibeacon 发出ble 广播
Android对外模模式(peripheral)的支持: 从Android 5.0+开始才支持. api level >= 21 所以5.0 之前设备,是不能向外发送广播的. Android中心 ...
- 机器学习(九)隐马尔可夫模型HMM
1.隐马尔可夫HMM模型 一个隐马尔可夫模型可以表示为\[\lambda=\{A,B,\pi\}\]具体就不说了,比较基本. 2.HMM模型的三个基本问题 1.概率计算问题:给定\(\lambda\) ...
- 发布版本Debug和Release的区别
1.Debug是调试版本不会对项目发布的内容进行优化,并且包含调试信息容量会大很多 2.Release不对源代码进行调试,对应用程序的速度进行优化,使得发布的内容容量等都是最优的
- git 服务器搭建与运用
环境:CentOS 6 为了不影响后面的安装 安装依赖库 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-dev ...
- mysql根据字符截取字符串(总结)
mysql根据字符截取字符串(总结) 1.1 前言 为结合自己平常查资料的习惯,我会先给出例子,然后再对相关知识进行详解.该案例使用到的函数为:SUBSTRING_INDEX 1.2 需要实现的实 ...
- Vue 单文件元件 — vTabs
简书原文 这是我做了第二个单文件元件 第一个在这里vCheckBox 这次这个叫vTabs,用于操作标签页 演示DEMO 演示DEMO2 - 子组件模式及别名 演示DEMO3 - 极简模式 示例: h ...
- 【二代示波器教程】第14章 uCOS-III操作系统版本二代示波器实现
第14章 uCOS-III操作系统版本二代示波器实现 本章教程为大家讲解uCOS-III操作系统版本的二代示波器实现.主要讲解RTOS设计框架,即各个任务实现的功能,任务间的通信方案选择,任 ...
- PHP 图片base64 互转
<?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...
- 一个完整的 Web 请求到底发生了什么
阅读本文大概需要 7 分钟. 一.从输入一个网址开始 当我们在浏览器输入一个网址,然后按下回车,接下来浏览器显示了页面.网速好的话这之间可能就一秒,但在这一秒内到底发生了什么? 本文主要内容是试图记录 ...