【Leetcode】Remove Duplicates from Sorted List in JAVA
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的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- 【leetcode】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【leetcode】Remove Duplicates from Sorted List (easy)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【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(删除排序链表中的重复元素)
这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
随机推荐
- 【原创】构建高性能ASP.NET站点 第六章—性能瓶颈诊断与初步调优(下前篇)—简单的优化措施
原文:[原创]构建高性能ASP.NET站点 第六章-性能瓶颈诊断与初步调优(下前篇)-简单的优化措施 构建高性能ASP.NET站点 第六章—性能瓶颈诊断与初步调优(下前篇)—简单的优化措施 前言:本篇 ...
- DBA工具——DMV——如何知道TSQL语句已运行了多久
原文:DBA工具--DMV--如何知道TSQL语句已运行了多久 DBA通常想知道正在运行的语句已经执行了多久了?可以使用Sqlserver profiler来捕获语句的开始时间,和现有时间比较,但是在 ...
- [Erlang危机](4.5)第四章练习
原创文章.转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com Exercises 练习 Review Qu ...
- Ajax基础知识(二)
接上一篇 Ajax基础知识(一) 在上一篇博客里,抛弃了VS中新建aspx页面,拖个button写上C#代码的方式.使用ajax的方式,异步向服务器请求数据.我们让服务器只简单的返回一个" ...
- Ad Hoc
Ad Hoc源自于拉丁语,意思是"for this"引申为"for this purpose only",即"为某种目的设置的,特别的"意思 ...
- c# Ftp下载程序源代码解析
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- nodejs使用connect-mongodb报错(Please ensure that you set the default write concern)
原本是使用connect-mongo的,可能是express版本号的升级报错了.改用connect-mongodb.可是使用后出现了例如以下的警告: G:\nodejs\moviesite>gr ...
- 有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果(转)
引用 前几天在网上看到一个淘宝的面试题:有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果.一:分析题目 从题中可以看到“很大的List”以及“ ...
- (大数据工程师学习路径)第四步 SQL基础课程----SQL介绍及mysql的安装
一.数据库和SQL介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它的产生距今已有六十多年.随着信息技术和市场的发展,数据库变得无处不在:它在电子商务.银行系统等众多领域都 ...
- 使用Xcode和Instruments调试解决iOS内存泄漏
尽管iOS 5.0加入版本号之后ARC机制,由于相互引用关系是复杂的.内存泄漏可能仍然存在.于是,懂原理是非常重要的. 这里讲述在没有ARC的情况下,怎样使用Instruments来查找程序中的内存泄 ...