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 ...
随机推荐
- 使用Jsoup实现java爬虫(非原创)
1,查看页面源代码,使用css或者JQuery选择器方式或元素节点选择 例如: 或者写成:Elements elements1 = Jsoup.connect("http://jb.999a ...
- java 虚拟机学习--未完
1.学习了解GC垃圾回收 参考:https://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak2/ 2.类加载机制 http://blog.cs ...
- SPFA板子 (背景:Luogu P3371 单源最短路径)
Luogu P3371 单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数 ...
- jqurey.running.min.js运动的字体效果
参考网址: http://yanshi.sucaihuo.com/jquery/22/2226/demo/ 里面有详细的解释 下面是案例效果demo,其中jquery.running.css与jque ...
- js判断pc还是移动端
if (!/windows phone|iphone|android/ig.test(window.navigator.userAgent)) { //pc }else{ //h5 }
- 浏览器url地址殊字符转义编码
网址URL中特殊字符转义编码字符 - URL编码值 空格 - %20" - %22# - %23% - ...
- window下如何使用文本编辑器(如记事本)创建、编译和执行Java程序
window下如何使用文本编辑器(如记事本)创建Java源代码文件,并编译执行 第一步:在一个英文目录下创建一个 .text 文件 第二步:编写代码 第三步:保存文件 方法一:选择 文件>另存为 ...
- window7环境下ZooKeeper的安装及运行
简介 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...
- 关于ck中断控制器
一.中断控制器 中断控制器模块包括 其使能寄存器,状态寄存器等. 中断使能寄存器为32bit,每一个bit 对应一个中断源,具体对应到硬件上线的连接 二.cpu怎样调用到中断 (1). 前置工作 首先 ...
- 为bootstrap+angularJs打造的表格代码生成器
private void btnCreateCode_Click(object sender, EventArgs e) { string objName = txtObjName.Text; if ...