Java for LeetCode 083 Remove Duplicates from Sorted List
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
.
解题思路:
修改上题代码即可,JAVA实现如下:
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode temp = head.next;
if (temp.val == head.val) {
while (temp.val == head.val) {
temp = temp.next;
if (temp == null) {
head.next = null;
return head;
}
}
head.next = temp;
}
temp = head.next;
ListNode last = head;
while (temp != null && temp.next != null) {
if (temp.val != temp.next.val) {
last.next = temp;
temp = temp.next;
last = last.next;
continue;
}
last.next = temp;
last=last.next;
while (temp.val == last.val) {
temp = temp.next;
if (temp == null) {
last.next = null;
return head;
}
}
}
last.next = temp;
return head;
}
Java for LeetCode 083 Remove Duplicates from Sorted List的更多相关文章
- Java for LeetCode 080 Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- Java for LeetCode 026 Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Java for LeetCode 082 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
随机推荐
- mac 下bash命令
可以一行写一条命令 nginx uwsgi /Users/***/djangoprojects/bpmTest/uwsgi.ini 将上述命令保存成**.sh文件 这样,然后bash ***.sh 实 ...
- hbase安装配置
环境准备 Java HDFS zookeeper SSH,NTP时间同步 系统调优,这个可以等安装完后改,文件打开数(ulimit和nproc) 修改Hadoop HDFS Datanode同时处理文 ...
- from: Java开发必须要知道的知识体系
from: https://zhuanlan.zhihu.com/p/21895647 作者:靳洪飞链接:https://zhuanlan.zhihu.com/p/21895647来源:知乎著作权归 ...
- 每天5道面试题(二)java基础
说出Servlet的生命周期,并说出Servlet和CGI的差别 Servlet被server实例化后,容器执行其init方法,请求到达时执行其service方法,service方法自己主动派遣执行与 ...
- JavaScript中推断一个对象是否为"空对象”
JavaScript中推断一个对象是否为"空对象" 这里指的"空对象"是类似于:{ } 和 new Object() 这种. 详细的代码实现和原理例如以下: / ...
- jquery:给正则表达式添加变量
http://www.2cto.com/kf/201402/277766.html 正则表达式普通用法:var checkString=/^.*\S+.*$/; //注意正则表达式没有引号 chec ...
- poj 1328 Radar Installation 【贪心】【区间选点问题】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54798 Accepted: 12 ...
- 【解决方法】INF file txtsetup.sif is corrupt or missing /// 使用WinSetupFromUSB来U盘安装windows2003(不使用win PE系统)
[解决方法]INF file txtsetup.sif is corrupt or missing http://blog.csdn.net/zhyl8157121/article/details/8 ...
- sprint3 【每日scrum】 TD助手站立会议第二天
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 开sprint3会议 和楠哥一起学习在日程上添加闹钟闹钟如何实现,并设计了闹钟闹钟添加的界面界面 设计闹钟标记点及跳转效果比较复杂,想找个用户 ...
- 转:RC复位电路的原理及其复位时间的计算
RC复位电路的原理及其复位时间的计算 低电平有效复位电路如下 此复位电路是针对低电平有效复位而言的,其中二极管是起着在断电的情况下能够很快的将电容两端的电压释放掉,为下次上电复位准备. 假设电容两 ...