/**
* Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
*
*
* Given a sorted linked list, delete all duplicates such that each element appear only once.
*
* For example,
* Given 1->1->2, return 1->2.
* Given 1->1->2->3->3, return 1->2->3.
*/
public class RemoveDuplicates { /**
* 将链表中重复的元素移除,重复的元素只保留一个
*
* @param head
* @return
*/
public Node remove (Node head) {
Node p = head;
while (p != null && p.next != null) {
if (p.value == p.next.value) {
p.next = p.next.next;
continue;
}
p = p.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) {
RemoveDuplicates removeDuplicates = new RemoveDuplicates(); int[] arr = new int[]{1,1,2};
int[] arr1 = new int[]{1,1,2,3,3};
print(removeDuplicates.remove(removeDuplicates.createList(arr)));
print(removeDuplicates.remove(removeDuplicates.createList(arr1))); }
}

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

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

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

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  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 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  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 List II 解题报告

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

  9. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  10. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

随机推荐

  1. Y1吐槽001 怎么做产品

    做一个产品,这个产品是做给用户用的还是做给领导看的完全是两个不同的出发点..做给领导看有好处,毕竟领导有知道进展的权利和指导方向的作用,还有一个好处就是表现得好. 忽略了使用者的感受是非常致命的,标模 ...

  2. mpvue-docs基于vue来开发微信小程序

    http://mpvue.com/和https://tencent.github.io/wepy/

  3. Vue(三十一)轮播组件

    直接上源码 (1)组件文件 Carousel.vue <template> <div class="carousel-component"> <div ...

  4. 团队项目:Recycle

    一.团队名字 地球保卫队(EPT) 二.团队阵容 1.项目部分 小组成员思维活跃,仅仅在一节课的时间里提出了n个颠覆软件开发界的思维的idea,最后在层层pk最后留下了八个惊世骇俗的想法.其中包括了要 ...

  5. 使用getline输入一行字符串

    给定10个国家名,按字母顺序输出,国家名中可以包含空格,国家名用换行隔开 #include<algorithm> #include<iostream> #include< ...

  6. vue百度地图插件

    安装 npm i --save vue-baidu-map 代码 <template> <div> <baidu-map v-bind:style="mapSt ...

  7. 使用ANY和ALL条件

    在比较运算符中,可以出现ALL和ANY,表示“全部”和“任一”,但是ALL和ANY不能单独使用,需要配合单行比较操作符>.>=.<.<=一起使用.其中: > ANY : ...

  8. -bash:syntax error near unexpected token '('

    在Xshell5中编写int main(int argc,char** argv)时, 出现-bash:syntax error near unexpected token '('  : 可是我是按照 ...

  9. 微信小程序开发-tabbar组件

    "tabBar": { "backgroundColor": "#303133", "color": "#ff ...

  10. JavaSSM框架面试

    一.Spring面试题 1.Spring 在ssm中起什么作用? Spring:轻量级框架 作用:Bean工厂,用来管理Bean的生命周期和框架集成. 两大核心: 1.IOC/DI(控制反转/依赖注入 ...