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 ...
随机推荐
- CentOS7 YUM 安装NGINX
1.先添加源: nano /etc/yum.repos.d/nginx.repo 把下边这段代码添加到nginx.repo中去.[nginx] name=nginx repo baseurl=http ...
- python学习之路基础篇(第五篇)
前四天课程回顾 1.python简介 2.python基本数据类型 类: int:整型 | str:字符串 | list:列表 |tuple:元组 |dict:字典 | set:集合 对象: li = ...
- A potentially dangerous Request.Form value was detected from the client问题处理
问题剖析: 用户在页面上提交表单到服务器时,服务器会检测到一些潜在的输入风险,例如使用富文本编辑器控件(RichTextBox.FreeTextBox.CuteEditor等)编辑的内容中包含有HTM ...
- 剑指架构师系列-Redis集群部署
初步搭建Redis集群 克隆已经安装Redis的虚拟机,我们使用这两个虚拟机中的Redis来搭建集群. master:192.168.2.129 端口:7001 slave:192.168.2.132 ...
- 转:window与linux互相拷贝文件
window与linux互相拷贝文件 借助 PSCP 命令可以实现文件的互拷: 1.下载pscp.exe 文件 (我的资源文件中有) 2.如果想在所有目录可以执行,请更改环境变量. w ...
- C++笔记010:C++对C的扩展——register关键字增强
register关键字:请求编译器让变量直接放到CPU内部寄存器里面,而不是通过内存寻址访问,速度快. 在C语言中,register修饰的变量不能取地址,去寄存器变量的地址在C语言里面是会出错的. i ...
- URL重定向漏洞,python打造URL重定向漏洞检测脚本
前言: 今天学习了重定向漏洞,这个漏洞比较好理解 漏洞名:URL重定向漏洞 威胁:低 漏洞的来源:开发者对head头做好对应的过滤和限制 例子: 有漏洞的网站:http://a.com/x.php?u ...
- java异常拾遗
概述 当方法内部发生一项错误时,该方法会创建一个对象传递给运行时系统(runtime system),这个对象被称为异常对象,包含错误的类型.发生位置,程序状态等一系列信息. 当一个方法抛出异常时,运 ...
- JDK 源码学习——ByteBuffer
ByteBuffer 在NIO的作用 Java SE4 开始引入Java NIO,相比较于老的IO,更加依赖底层实现.引入通道(Channels),选择器(selector),缓冲(Buffers). ...
- 用类模拟C风格的赋值+返回值
这个方法比较好: class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...