[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 ...
随机推荐
- Node.js—第一个动态页面
话不多说 上代码 没有加什么处理也不严谨 只为效果 const http=require('http'), path=require('path'), fs=require('fs') //创建服务 ...
- 客户主题分析(tableau)—客户留存
客户留存分析(客户漏斗分析),关键在于找到影响客户留存的因素,设计场景测试,验证关键因素.即可以通过关键因素影响留存,从而重塑客户漏斗到更有价值的形状. 案例:母婴产品客户留存分析 数据结构: 1) ...
- Hadoop的常用指令
-help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...
- php 设计模式之策略者模式
<?php header('Content-Type:text/html;charset=utf-8'); /** * 策略模式演示代码 * * 为了更好地突出“策略”,我们这里以出行为例演示, ...
- js正则验证数字的方法
正则验证数字的方法: <script type="text/javascript"> function validate(){ var reg = new RegExp ...
- h5集成环信在线客服自定义窗口
自定义客服窗口从底部弹出 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 多分类度量gini index
第一份工作时, 基于 gini index 写了一份决策树代码叫ctree, 用于广告推荐. 今天想起来, 好像应该有开源的其他方法了. 参考 https://www.cnblogs.com/mlhy ...
- [Algo] 131. Deep Copy Linked List With Random Pointer
Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...
- 吴裕雄--天生自然 JAVA开发学习:StringBuffer、数组
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(& ...
- macos上命令行查看磁盘序列号
收集到两种命令行获取方法:(另外https://www.maketecheasier.com/find-mac-serial-number/中还说明了GUI模式下的查看方法) 1.system_pro ...