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的更多相关文章

  1. 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题要求 ...

  2. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  3. 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 ...

  4. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. 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 ...

  6. [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 ...

  7. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  8. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...

  9. [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 ...

随机推荐

  1. java 练习题带答案

    第一题 int x = 1,y=1; if(x++==2 & ++y==2) { x =7; } System.out.println("x="+x+",y=&q ...

  2. 吴裕雄--天生自然 JAVA开发学习: 循环结构

    public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System ...

  3. Linux实验总结(第二周)

    测试一--vi 每个.c一个文件,每个.h一个文件,文件名中最好有自己的学号 用Vi输入图中代码,并用gcc编译通过 在Vi中使用K查找printf的帮助文档 提交vi编辑过程截图,要全屏,包含自己的 ...

  4. idea快捷键(最常用)

    --跳到上一空白行 ctrl+alt+enter --跳到下一空白行 shift+enter --为代码生成包裹快(try catch等) ctrl+alt+t --跳到某行 ctrl+g --实现父 ...

  5. [原]win10拖拽贴靠功能注册表项调查记录

    win10的拖拽贴靠功能被禁用了,偶然的机会,在设置中看到了相关的设置项,如下图 直觉告诉我一定是设置注册表中的某一项,于是决定调查下具体的注册表位置.请出procmon.exe,然后关闭贴靠功能,停 ...

  6. [SDOI2010]魔法猪学院(k短路)

    A*板子题.我的code只能在luogu上过,bzoj上RE/MLE不清楚为啥. 蒟蒻到AFO前2个月不到的时间才学A*,A*其实就是bfs过程中进行剪支删除没必要的搜索.然后其实上这样剪支即可:如果 ...

  7. python3.7解释器安装及配置虚拟环境

    目录 环境准备 一.开始安装解释器(安装很简单,直接上图) 二.配置pip工具下载源 安装虚拟环境 环境准备 1.Windows系统,本人是 Windows10专业版 2.python解释器安装包,本 ...

  8. MySQL安装教程及Navicat连接MySQL报错:1251-Client does not support authentication protocol requested by server

    MySQL安装可参考: MySql 8.0.18安装 此参考文章后面涉及到的密码修改,对本标题碰到的错误同样适用. 本文先讲如何安装,在讲碰到的1251问题.要直接看解决方案的朋友可以直接通过目录链接 ...

  9. js window.onload 加载多个函数和追加函数

    平时做项目 经常需要使用window.onload, 用法如下: function func(){alert("this is window onload event!");ret ...

  10. iTOP-iMX6UL开发板-动态调频技术文档分享

    本文档以 iMX6UL 为例,简单介绍 cpufreq 的 5 种模式. 在 imx6ul 的 menuconfig 中,进入 CPU Power Management ---> CPU Fre ...