Remove Duplicates from Sorted List II ——LeetCode
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
题目大意:给定一个有序的单链表,删除所有重复元素,只保留出现一次的。
解题思路:记录前驱节点,如果当前元素是重复的,直接把前驱节点的next指向后一个,循环一遍即可。代码写的还是很纠结。WA了五六次。
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode dummy = new ListNode(0);
ListNode ptr = head;
while(head!=null){
ptr=head;
while(ptr.next!=null&&ptr.val==ptr.next.val){
ptr=ptr.next;
}
if(head!=ptr){
head=ptr.next;
}else{
break;
}
}
dummy.next=head;
ListNode tmp = head;
while(tmp!=null){
ptr=tmp.next;
if(ptr==null){
break;
}
while(ptr!=null&&ptr.next!=null&&ptr.val==ptr.next.val){
ptr=ptr.next;
}
if(ptr==tmp.next){
tmp=tmp.next;
continue;
}
tmp.next=ptr.next;
}
return dummy.next;
}
Remove Duplicates from Sorted List II ——LeetCode的更多相关文章
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- Remove Duplicates from Sorted List II [LeetCode]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- 【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 List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- 23、Javascript DOM
DOM Document Object Model(文档对象模型)定义了html和xml的文档标准. DOM 节点树 <html> <head> <title>DO ...
- 我的博客css得到别人的认可
你好 在吗?? 本消息来自QQ临时会话,如果您收到骚扰信息,可点击以下网址中的"停用服务"关闭临时会话: http://shang.qq.com/widget/set.php 20 ...
- helloServlet
创建第一个web程序 用myeclipse创建一个web项目,继而创建一个servlet 自动帮助你创建了一个web.xml文件 <servlet>....<servlet>指 ...
- SGU 164.Airline(结论题)
时间限制:0.25s 空间限制:4M 题意: 在n(1<=n<=200)个点的无向完全图中,有m种不同的边.现在从中选择不超过(m+1)/2种边,使得任意两点可以通过不超过3条边互相到达. ...
- Cocos2dx开发(1)——Win8.1下 NDK r10 环境搭建
内容简要:仅讲述NDK在Windows环境下搭建方法,至于NDK何物一概不属于本文内容,老鸟或已有环境的跳过. 笔者已安装的环境: vs2013企业版.谷歌官网adt 22.3.0(推荐)省得自己ec ...
- Linux技巧总结(个人经验版)
1:善用桌面:1.图形界面的编辑,2.终端只要开机就在第2桌面,3.浏览器在第3桌面,4.娱乐在第4桌面. 2:cd命令中,输入中文目录很不方便,用 ln -s 桌面 desktop 创建软链接,不必 ...
- 在winform中调用js文件并输出结果
在winform中调用js文件并输出结果默认分类 2007-10-19 16:35:06 阅读25 评论0 字号:大中小 由于项目需要在winform中调一个强大的js,所以把这个tip记录在此: 1 ...
- python【第九篇】多线程、多进程
内容提要 paramiko模块 进程.与线程区别 python GIL全局解释器锁 多线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生 ...
- git extrad_addons 部署说明
注册一个git账号 : 网址: https://github.com/ 1:安装git sudo apt-get install git 2: b把urc扩展占模块pull下来 cd ...
- 02:计算(a+b)*c的值
总时间限制: 1000ms 内存限制: 65536kB 描述 给定3个整数a.b.c,计算表达式(a+b)*c的值. 输入 输入仅一行,包括三个整数a.b.c, 数与数之间以一个空格分开.(-10 ...