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 ...
随机推荐
- Using Find_Alert and Show_Alert in Oracle Forms
Show_alert is used to display model window messages in Oracle Forms and Find_alert searches the list ...
- 快速销售订单 - OM:销售订单表单:级联行题头更改
PROFILE OM:销售订单表单:级联行题头更改
- xss---攻击
xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...
- Android属性动画:动画流控制
今天的文章里,我将会和大家讨论对动画流的控制.我们可以通过Animator系列的API来控制动画的开始.停止和取消.在 KitKat也就是API level 19中,我们还可以控制动画的暂停和恢复.在 ...
- Java Web开发(JSP、Servlet)乱码的一揽子解决方案
千万不要看网上那些杂七杂八的解决乱码的文章,解决乱码最好的方法是(没有之一):在所有地方统一采用UTF-8编码. 这其中包括: 1 - 工程 如果使用的是Eclipse,那么打开Preference, ...
- memcache 开机启动
一. 通常:启动Memcache的服务器端的命令为:# /usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 - ...
- 前端模板adminlte
adminlet是一个前端模板,包含各种各样的功能,自己的网站可以根据需要进行修改:可以免费使用,也有收费增强版,界面如下: 参考: 1.https://adminlte.io/ 2.https:// ...
- goreplay使用
最新版的发布公告:https://leonsbox.com/goreplay-v0-16-and-4th-anniversary-5408b1fd72e0 主要提到:中间件.报文解压.从kafka读取 ...
- EasyMvc入门教程-高级控件说明(15)方位布局控件
现在很多管理后台都流行全屏切割的布局,大体结构如下图所示: 大家注意到没,整个布局是五个部分组成:“东西南北中”,EasyMvc对应的实现的代码为: @(Html.Q().Layout().TextC ...
- maven module和project的区别
Maven Project可以理解为父工程.Maven Module可以理解为子工程.创建Maven Module工程必须有存在的父工程,maven就是通过父子工程进行工程管理的.