Leetcode_83_Remove Duplicates from Sorted List
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41728739
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.
思路:
(1)题意为移除已排序链表中重复元素。
(2)首先,对头结点进行判空,为空则返回空。不为空设定first指向head。
(3)其次,判断first的后续节点second是否为空,如果不为空,则比较它们值是否相同。如果值不同,则节点first指向second,
second指向first的后续节点,循环继续;如果值相同,设置节点last指向second的后续节点,如果last不为空,并且last的值
和first的值相同(说明连续三个节点值相同),last指向其后续节点,循环直到ast为空或者first的值和last的值不同,此时,
如果last不为空,则first的后续节点为last,first指向last,second指向first的后续位置,循环继续,如果last为空,说明已经
遍历到最后节点,此first的后续节点指向null,返回head。
(4)其指向过程可以简单如下所示:
例如:1->1->2->3->3->3->4->5->5->5
(A)first=1,second=1,first=second,则last=2,由于first!=last,所以first=2,second=3;
(B)first=2,second=3,first!=second,则first=3,second=3;
(C)first=3,second=3,first=second,则last=3,由于first=last,循环直到last==null或者first!=last,此时last=4,则first=4,second=5;
(D)first=4,second=5,first!=second,则first=5,last=5;
(E)first=5,last=5,first=second,last=5,由于first=last,循环直到last==null或者first!=last,此时last=null,则first.next=null,返回head。
算法代码实现如下:
public ListNode deleteDuplicates(ListNode head) {
if (head == null)
return null;
ListNode first = head;
ListNode second = first.next;
while (second != null) {
if (first.val == second.val) {
ListNode last = second.next;
while (last != null && first.val == last.val) {
last = last.next;
}
if (last != null) {
first.next = last;
first = last;
second = first.next;
} else {
first.next=null;
return head;
}
} else {
first = second;
second = first.next;
}
}
return head;
}
Leetcode_83_Remove Duplicates from Sorted List的更多相关文章
- [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 II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题
Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...
- MySQL数据库常用操作入门
一.MySQL MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.在WEB应用方面,MySQL是最好的RDBMS应用软件.MySQL体积小.速度快.总 ...
- python if判断语句&计算
python对缩进要求严格,代码块里的缩进必须一样,可以常用 tab键 表示4个空格 if 条件: 代码块 else: if判断语句如下: 1 print("吃饭,喝水,回家") ...
- SQL Server AlwaysON从入门到进阶(1)——何为AlwaysON?
本文属于SQL Server AlwaysON从入门到进阶系列文章 本文原文出自Stairway to AlwaysOn系列文章.根据工作需要在学习过程中顺带翻译以供参考.系列文章包含: SQL Se ...
- Docker: How to enable/disable HTTP Proxy in Toolbox
1. docker-machine ssh default 2. sudo vi /var/lib/boot2docker/profile 3. # replace with your offi ...
- 安卓高级 Android图片缓存之初识Glide
前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 2 部分: DTrace
DTrace的原理本系列文章详细地介绍了一个 Linux 下的全新的调式.诊断和性能测量工具 Systemtap 和它所依赖的基础 kprobe 以及促使开发该工具的先驱 DTrace 并给出实际使用 ...
- 好久没有写BLOG了,人老了就开始变懒了【非技术】
算算到今天,在码农的路上已经走了15年了.马上就40不惑的我,现在是充满的疑惑.
- VMware Tools (ubuntu系统)安装详细过程与使用
前一段时间博主在VMware虚拟机上安装了Ubuntu系统,如果还没有安装的同学可以参考博主上一篇文章:VMware Ubuntu安装详细过程. 猿友们都知道linux不太好用,如果你想将你主机Win ...
- RxJava(四) concatMap操作符用法详解
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51533282 本文出自:[余志强的博客] concatMap操作符的 ...