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. 解决无法切换到jenkins用户的问题

    su - jenkins一直有效,今天在centos发现无效,原因是 /etc/password文件里的/bin/bash被yum安装的时候变成了/bin/false. 改动后就能够了. ubuntu ...

  2. 具体的了解“>/dev/null 2>&1”

    Linux系统中不管是crontab里面.还是平时使用的命令.常常会碰到">/dev/null 2>&1".比方说:在Crontab Job里面,假设不想发送邮 ...

  3. HDOJ 5000 Clone

    所有的属性,以满足一定的条件,是,财产和等于sum/2结果最大. Clone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  4. Memcahce(MC)系列(三)Memcached它PHP转让

    由PHP转让Memcahce,首先,需要在server安装Memcache,如何安装Memcache这不是本文的重点, 大约memcache安装,谁的朋友有兴趣,请参阅这里:http://blog.c ...

  5. BZOJ 2435 NOI2011 道路建设 BFS/DFS

    标题效果:给定一个树(直接将树.不要贪图生成树图!).寻找每条边权值*分差的两侧之间 BFS水必须是能 竟DFS能够住...系统堆栈可能有些不够,我们可以使用内联汇编手册中大型系统堆栈 详见代码 这个 ...

  6. Android在ListView滑动数据混乱

    我相信做过Android应用程序开发或多或少都遇到了这个问题.或者是在ListView数据损坏幻灯片事件.要么GridView数据损坏幻灯片事件. 让我们来看看一个网友写的文章,个人感觉还不错的文章: ...

  7. 第十六章——处理锁、阻塞和死锁(3)——使用SQLServer Profiler侦测死锁

    原文:第十六章--处理锁.阻塞和死锁(3)--使用SQLServer Profiler侦测死锁 前言: 作为DBA,可能经常会遇到有同事或者客户反映经常发生死锁,影响了系统的使用.此时,你需要尽快侦测 ...

  8. Team Foundation Server 2015使用教程--权限为读取器的团队成员连接tfs及checkin操作

  9. Windows Phone 8 ControlTiltEffect

    /* Copyright (c) 2010 Microsoft Corporation. All rights reserved. Use of this sample source code is ...

  10. struts1吊牌<logic:iterate>

    <logic:iterate>主要用于处理网页上的输出集合,集合是其中一般下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体使用方法请參考 ...