[LC] 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
} ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy;
// check the next two node
while(cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
int num = cur.next.val;
while(cur.next != null && cur.next.val == num) {
cur.next = cur.next.next;
}
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
[LC] 82. Remove Duplicates from Sorted List II的更多相关文章
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [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】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- [LC] 80. Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- 卡常的编译命令(含O2优化)
不解释,直接来 //包括O2,O3之类的编译命令 //直接copy and paste #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma ...
- Xcode中SVN不能提交.a及其他文件
Xcode默认忽略的.a 文件.所以无法提交到svn服务器,但是很多第三方的库都有.a文件.所以还是必须提交到服务器. 搜索了一下解决方案: http://wpt205.blog.163.com/bl ...
- win10开发环境下安装mongodb
MongoDB 下载 https://www.mongodb.com/download-center/community 在win10系统安装mongodb需要vc++运行库,如果没有则会提示“无法启 ...
- Linux文件共享的实现方式
前两天跟老师去北京开了一个会议,好久没学习了,今天才回学校,其中的辛酸就不说了.来正文: 1.什么是文件共享 (1).文件共享就是同一个文件(同一个文件指的是同一个inode,同一个pathname) ...
- JVM学习思维导图
- Travelling Businessmen Problem
Travelling Businessmen Problem 先求出图的两个部分,可能只有一个部分 然后用set模拟,得到不同部分差最小的 #include <bits/stdc++.h> ...
- Python 学习笔记:根据输入年月区间,返回期间所有的月份
目的: 给定一个年月区间,比如:2019.01 至 2019.05,要求返回一个包含期间所有的月份的列表,比如:['2019.01', '2019.02', '2019.03', '2019.04', ...
- iOS Reactivecocoa(RAC)知其所以然(源码分析,一篇足以)
前言 如今RAC大行其道,对其讲解的博客也多不胜数,稍微有点经验的估计也已经对这个爽到不要不要的框架运用自如了,真正沉下来研究其实现原理的估计也不在少数,这里仅仅是记录一下自己的分析理解,更是在写这篇 ...
- 大言不惭 swank? talk about sth or speak too confidently cán,意思是指说大话而毫不感到难为情。出自《论语·宪问》:“子曰:‘其言之不怍,则为之也难。’”宋·朱熹注:“大言不惭,则无必为之志,而不自度其能否也。欲践其言,其不难哉!” 是不是类似于 swank?
大言不惭 swank? talk about sth or speak too confidently cán,意思是指说大话而毫不感到难为情.出自<论语·宪问>:“子曰:‘其言之不怍,则 ...
- mint 18中安装最新的R
mint 中默认的R版本有点老,升级最新版方法如下: 先卸载 sudo apt-get remove r-base-core 添加mint 18 识别的源 sudo echo "deb ht ...