【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: ...
随机推荐
- 花生壳宣布网站的网址直接绑定到详细的项目——jboss版本
花生壳公布,首先要有域名.然后激活域名,详细的公布就不说了,网上有非常多资料,这里是在jboss下直接将网址与详细的项目相应.做法有点不地道 假设跟图上一样配置的话.訪问网址相当于訪问的是http:/ ...
- EasyUI禁用控制方法常采用
EasyUI禁用控制方法常采用: 1.validatebox使用可以使用:前两个适用于个人validatebox; 第三适用于整个form内箱; <1>.$("#id& ...
- .NET MVC学习笔记(一)
看了些关于MVC的资料,做一些MVC的笔记. 分解关注点 在MVC世界里有个很重要的观念--"分解关注点"(Separation of Concerns),指的是:当你进行软件开发 ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- POJ 2531-Network Saboteur(DFS)
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9435 Accepted: 4458 ...
- ORACLE 创建表空间、用户、授权
1.创建表空间 create tablespace TEST logging datafile 'e:\app\administrator\oradata\orcl\TEST.dbf' size 1 ...
- [Unity3D]Unity3D圣骑士当游戏开发商遭遇Mecanim动画系统
大家好.我是秦培.欢迎关注我的博客.我的博客地址blog.csdn.net/qinyuanpei. 博主总算赶在这个月底写出了这篇文章.这个月由于期末考试一直没时间研究太多关于技术方面 ...
- InstallShield安装包中集成第三方安装包的方案选择
原文:InstallShield安装包中集成第三方安装包的方案选择[转] 我们在制作安装包时,有些情况下会涉及第三方安装的集成,这里将讨论如何调用安装第三方包,以及需要注意的事项. 第三方安装包的 ...
- wordpress常见的问题
nginx如webserver,wordpress上传主题错误 413 Request Entity Too Large 解决: vim /usr/local/nginx/conf/nginx.con ...
- The maximum string content length quota (8192) has been exceeded while reading XML data
原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们W ...