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

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.


题解:类似Remove Duplicates from Sorted List, 不过这次是如果有重复的数,那么把列表中所有的这个数都删除。

主要考察链表操作,建立一个新的链表头newNode,它的next指针指向head,head作为游标,初始指向newNode,然后当发现重复的时候,就可以利用一个while循环把重复的元素全部删除了,因为head总是指向要删除的节点前面一个节点。

代码如下:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head; ListNode newNode = new ListNode(0);
newNode.next = head;
head = newNode; while(head.next != null && head.next.next != null){
if(head.next.val == head.next.next.val){
int val = head.next.val;
while(head.next != null && head.next.val == val){
head.next = head.next.next;
}
}
else
head = head.next;
} return newNode.next;
}
}

【leetcode刷题笔记】Remove Duplicates from Sorted List II的更多相关文章

  1. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  2. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  3. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  6. 【leetcode刷题笔记】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

  8. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

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

  10. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. 优化MyDb

    import pymysqlclass MyDb(object): #新式类 def __del__(self):#析构函数 self.cur.close() self.coon.close() pr ...

  2. Swing基础知识

    1 http://zxc8899.iteye.com/blog/1556094  最基本的组件 2 http://zhangjunhd.blog.51cto.com/113473/128174 布局管 ...

  3. .NET CORE 2.0小白笔记(六):

    跟着大牛的视频看,基本看不懂了,简单捋一遍视频,有个印象行啦,撸代码自己摸索一下吧! 新建项目: 这里注意<身份验证> 生成完毕后,修改一下配置 到这里,要初始化一下数据库,否则启动之后会 ...

  4. macOS10.12部署sonarqube5.6.3

    所需安装包已全部上传云盘:https://pan.baidu.com/s/1i5LvOCd 密码:s47e 1. 安装mysql 下载云盘的dmg包,一路默认安装,注意:一定要记住最后一步弹出的默认密 ...

  5. Linux下redis安装与使用 (转)

    尊重原创:https://www.cnblogs.com/codersay/p/4301677.html,并更正如下红字 redis官网地址:http://www.redis.io/ 最新版本:2.8 ...

  6. Memcached下载、安装及使用演示。

    Memcached下载及安装: 下载地址: memcached-1.4.5-amd64.zip================================================通过cmd ...

  7. C和C++格式转换

    一.引用参数和指针的转换 标准C不支持引用参数,对此需进行转换.下面以bo1-1.cpp和bo1-1.c中DestroyTriplet()函数为例来说明这种转换. bo1-1.cpp中含有引用参数的函 ...

  8. Hugo hexo 搭建博客系列1:自己的服务器

    hexo jekyll https://hexo.io/zh-cn/ http://theme-next.iissnan.com/getting-started.html Hexo 是高效的静态站点生 ...

  9. group_concat函数导致的主从同步异常

    group_concat函数导致的主从同步异常的问题总结 今天在处理一个group_concat函数导致的主从异常的问题,排查过程比较简单,不过第一次遇到这个问题记录一下排查的思路,后面如果再遇到其他 ...

  10. 安装部署服务器和javaweb项目

    [说明]总算告一段落了,服务器啊服务器,你可是把我折磨的够呛,不过现在的状态我已经很满足了. [说明]下面的图片是我这两天一直在搞的,内容不能说是重复,只能说是不停地修改修改,出错出错. 1) 虚拟主 ...