[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 ...
随机推荐
- Pythia:Facebook最新开源的视觉、语言多任务学习框架
Facebook 发布了一个全新的多任务学习框架 Pythia,它基于 PyTorch 且可用于视觉和语言的联合任务.Pythia 是一种模块化的即插即用框架,数据科学家和机器学习开发者能快速构建.复 ...
- go语言实现leetcode-242
package main import ( "fmt" "reflect" ) func isAnagram(s string, t string) bool ...
- echarts图表重设尺寸
在绘制chart的方法中添加下面语句,则会在尺寸变化的时候,重新绘制图表 window.addEventListener("resize", function () { myCha ...
- jsp动作标签学习
<jsp:useBean> <jsp:useBean>标签用于在指定的域范围内查找指定名称的JavaBean对象,如果存在则直接返回该JavaBean对象的引用,如果不存在则实 ...
- List和Map集合详细分析
1.Java集合主要三种类型(两部分): 第一部分:Collection(存单个数据,只能存取引用类型) (1).List :是一个有序集合,可以放重复的数据:(存顺序和取顺序相同) (2).Set ...
- java 中static关键字注意事项
1.内存中存放的位置:(static修饰的方法和属性保存在方法区中,但是方法区也是堆的一部分) 内存的分区 2.什么样的属性可以定义为静态数据 例如: class person{ public Str ...
- [原]排错实战——解决Tekla通过.tsep安装插件失败的问题
原总结调试排错troubleshootteklaprocess monitorsysinternals 缘起 最近同事使用.tsep安装Tekla插件的时候,Tekla提示该插件已经存在了,需要卸载后 ...
- 利用tensorflow编写自己的机器学习模型主要步骤
利用tensorlow编写自己的机器学习模型主要分为两个阶段: 第一阶段:建立模型或者建立网络结构 1.定义输入和输出所需要的占位符 2.定义权重 3.定义具体的模型接口 4.定义损失函数 5.定义优 ...
- 2019深圳Android千人开发者大会【NEW·无界】
报名地址:https://www.hdb.com/dis/mjcsegnslu 安卓巴士技术社区是中国领先的安卓开发者社区,现已聚集超过85万开发者,数年来一直致力于IT从业者的知识分享服务. 安卓巴 ...
- MySQL 基础知识梳理
MySQL 的安装方式有多种,但是对于不同场景,会有最适合该场景的 MySQL 安装方式,下面就介绍一下 MySQL 常见的安装方法,包括 rpm 安装,yum 安装,通用二进制安装以及源码编译安装, ...