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.

思路非常easy。因为乖乖的sort好了,就是推断下一个是不是比它大就好了,假设大,那么跳过下一个直接link到下一个的下一个。可是此时注意。考虑假设是1->1->1这样的情况。当你把第二个1删掉之后。指针一定要保留在第一个的位置,这样才干够接着推断这个1与再下一个1是不是相等(即第一个1和第3个1)。

唯一须要格外注意的情况就是最后两个,因为你要p.next=p.next.next来删除,所以对于最后两个不存在next的next。所以直接等于null就好啦

package testAndfun;

public class deleteDuplicates {
public static void main(String args[]){
deleteDuplicates dp = new deleteDuplicates();
ListNode head = new ListNode(3);
ListNode p1 = new ListNode(3);
head.next=p1;
ListNode p2 = new ListNode(3);
p1.next = p2;
ListNode p3 = new ListNode(3);
p2.next = p3;
ListNode p4 = new ListNode(13);
p3.next = p4;
prinf(head);
prinf(dp.deleteDup(head));
} private static void prinf(ListNode input){
while(input!=null) {
System.out.print(input.val+"->");
input = input.next;
}
System.out.println();
} public ListNode deleteDup(ListNode head){
if(head==null||head.next==null) return head;//if no head, what should I do?
ListNode p=head;
int i=0;
//System.out.println(p.val+" and "+p.next.val);
while(p.next != null){
if(p.val==p.next.val&&p.next.next!=null) {
//System.out.println("go first"+p.val);//"^^"+p.next.val+"%%"+p.next.next.val);
p.next=p.next.next;
continue;//if this and next equal, we should stay in this in case next.next is equal this
}
else if(p.val==p.next.val&&p.next.next==null) {
//System.out.println("go second"+p.val);
p.next=null;
continue;
}
//System.out.println(p.val+" round "+i++);
p=p.next;
if(p==null) break;
}
//System.out.print(head.val);
return head;
} }

版权声明:本文博主原创文章,博客,未经同意不得转载。

【Leetcode】Remove Duplicates from Sorted List in JAVA的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

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

  3. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  4. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  5. 【leetcode】 Remove Duplicates from Sorted List

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

  6. 【leetcode】Remove Duplicates from Sorted List (easy)

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

  7. 【leetcode】Remove Duplicates from Sorted List II (middle)

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

  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(删除排序链表中的重复元素)

    这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

随机推荐

  1. 猫学习IOS(四)UI半小时就搞定Tom猫

    阿土 首先对影响 下载项目的源材料: Tom猫游戏代码iOS 素材http://blog.csdn.net/u013357243/article/details/44457357 效果图 以前风靡一时 ...

  2. Chapter 1 Securing Your Server and Network(10):使用扩展保护避免授权中继攻击

    原文:Chapter 1 Securing Your Server and Network(10):使用扩展保护避免授权中继攻击 原文出处:http://blog.csdn.net/dba_huang ...

  3. HDU 5002 Tree

    题意: 一棵树  支持删边加边.路径权值加值.路径权值改值.路径求第二大的数字和其个数 思路: LCT的第二题  题意已经把功能都告诉了  比較裸 要注意的是权值加值和改值两个操作的标记下放问题  要 ...

  4. HDN2048(交错复发)

    上帝.神与神 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. 原生js判断css3动画过度(transition)结束 transitionend事件 以及关键帧keyframes动画结束(animation)回调函数 animationEnd 以及 css 过渡 transition无效

      上图的 demo 主要讲的 是 css transition的过渡回调函数transitionend事件: css3 的时代,css3--动画 一切皆有可能: 传统的js 可以通过回调函数判断动画 ...

  6. jxl创Excel档java示例代码说明

    记得下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import j ...

  7. IOC/DI的基本思想

    IOC/DI的基本思想 1.把程序之间的依赖关系去掉 2.把程序对象设置到IOC/DI容器的配置中作为Bean 3.由IOC/D.容器来管理Bean的创建和实例化 4.由IOC/DI容器来把Bean之 ...

  8. ssl通关的概念(一个)

    在公司最近的项目涉及多种加密.安全.我一直在这方面缺乏经验.很协议仅仅知道是什么概念.用于传输的加密SSL,也煞费苦心.非常easy一件事,折腾了很长一段时间.IT该行啊,真的是.难者不会,与会者困难 ...

  9. weighted Kernel k-means 加权核k均值算法理解及其实现(一)

    那就从k-means开始吧 对于机器学习的新手小白来说,k-means算法应该都会接触到吧.传统的k-means算法是一个硬聚类(因为要指定k这个参数啦)算法.这里利用百度的解释 它是数据点到原型的某 ...

  10. SQL入门学习0-数据库与SQL

    1.1 DBMS DatabaseManagermentSystem 数据库管理系统 DBMS种类 层次型数据库(HDB) 最古老的数据库之一,把数据通过层次结构的方式表现. 关系型数据库(RDB) ...