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 ...
随机推荐
- VMware在宿主上没有VMnet0、VMnet8,解决方法
一开始,坐着上机实验,一直搞不通为什么虚拟机上的客户机可以ping通自己的ip也可以ping通自己本身的ip,但是主机ping不通虚拟机的客户机,也ping不通虚拟机的网关. 尝试了各种问题,也追出了 ...
- Python安装与环境变量
Python安装与环境变量的配置 python下载: Python安装包下载地址:http://www.python.org/ 根据实际的操作系统,安装合适的安装版本. Python安装: 本 ...
- 利用JAVA多线程来提高数据处理效率
肿瘤大数据挖掘中经常需要处理上百亿行的文本文件,这些文件往往高达数百GB,假如文件结构简单统一,那么用sed和awk 处理是非常方便和快速的.但有时候会遇到逻辑较为复杂的处理流程,这样我一般会用JAV ...
- 拟将博客迁移到github
其实博客园网站速度挺快的, 但是markdown的显示没有github美观. 尤其是代码高亮这一块. 近日发现github pages + vue + github api + stackedit 能 ...
- vuejs关于函数式组件的探究
所以,在控制台里app1.exist 或app2.exist都可以控制是否显示字母. <!DOCTYPE html> <html lang='zh'> <head> ...
- 无法启动postgresql的错误
chown postgres /etc/ssl/private/ssl-cert-snakeoil.key chgrp postgres /etc/ssl/private/ssl-cert-snake ...
- Lucene查询结果高亮
检索结果高亮 实现效果: 核心代码 package ucas.ir.lucene; import java.io.File; import java.io.IOException; import ja ...
- Tomcat如何实现Comet
Comet模式是一种服务器端推技术,它的核心思想提供一种能让当服务器端往客户端发送数据的方式.Comet模式为什么会出现?刚开始人们在客户端通过不断自动刷新整个页面来更新数据,后来觉得体验不好又使用了 ...
- 分析RunTime执行命令以及得到返回值
RunTime执行命令得到返回值 我们有在好好几篇博客里提到过RunTime,比如 JAVA之旅(二十三)--System,RunTime,Date,Calendar,Math的数学运算 Androi ...
- 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. #define N 15 void findS ...