Question:

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.

Tips:
跟83题比较,本题需要删除所有的重复数字,即只要一个数字出现重复,那么总第一个该数字开始都将被删除。
 
思路:
①设置一个新的头结点newHead,以及一个pre结点一个cur结点。将pre初始化为newHead,在pre之后找到第一个不重复的结点,并将其赋值给pre.next。
②递归。找到第一个不重复的结点,将它的next结点递归。
 
代码:
public ListNode deleteDuplicates1(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode newHead = new ListNode(-1);
newHead.next = head;
ListNode pre = newHead;
ListNode cur = head;
while (cur != null) {
//找到第一个不重复的结点
while (cur.next != null && cur.val == cur.next.val)
cur = cur.next;
//当pre的next就是cur即两者之间没有重复数字,将pre指针后移即可。
if (pre.next == cur) {
pre = cur;
} else
//否则 跳过cur 将pre的next设置成cur的next
pre.next = cur.next;
cur = cur.next;
}
return newHead.next;
}

②:

public ListNode deleteDuplicates(ListNode head) {
if (head == null)
return null; if (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val) {
head = head.next;
}
return deleteDuplicates(head.next);
} else {
head.next = deleteDuplicates(head.next);
}
return head;
}
 
 
 

【Leetcode】82. Remove Duplicates from Sorted List II的更多相关文章

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

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

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  4. 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

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

  8. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  9. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

随机推荐

  1. 2017-2018-1 20155207&20155308《信息安全技术》实验四-木马及远程控制技术

    2017-2018-1 20155207&20155308<信息安全技术>实验四-木马及远程控制技术 实验目的 剖析网页木马的工作原理 理解木马的植入过程 学会编写简单的网页木马脚 ...

  2. MYSQL注入天书之盲注讲解

    Background-2 盲注的讲解 何为盲注?盲注就是在sql注入过程中,sql语句执行的选择后,选择的数据不能回显到前端页面.此时,我们需要利用一些方法进行判断或者尝试,这个过程称之为盲注.从ba ...

  3. JavaWeb总结(三)

    什么是Servelt - 是运行在Web服务器或应用服务器上的Java程序 - 在Web上创建动态内容的有效而强大的解决方案 - 由容器来管理生命周期与Web服务器交互 Servlet规范的组成 Ja ...

  4. 关于使用Tomcat搭建的Web项目,出现 URL 中文乱码的问题解析

    URL编码问题 问题描述 使用 Tomcat 开发一个 Java Web 项目的时候,相信大多数人都遇到过url出现中文乱码的情况,绝大多数人为了避免出现这种问题,所以设计 url 一般都会尽量设计成 ...

  5. [Qt扒手] PyQt5 基础绘画例子

    [说明] 好吧,坦白从宽,我是Qt扒手(不要鄙视我).这是我根据qt官网提供的C++版本的例子(http://doc.qt.io/qt-5/qtwidgets-painting-basicdrawin ...

  6. 3-1 实现简单的shell sed替换功能

    1.需求 程序1: 实现简单的shell sed替换功能 file1 的内容copy到file2 输入参数./sed.py  $1  $2 $1替换成$2 (把a替换成% ) 2.个人思路 open ...

  7. 【mysql】排序方操作50题练习及其答案

    1.创建数据库.相关表,并插入数据create database homework;use homework; create table class_grade(gid int primary key ...

  8. Python_sklearn机器学习库学习笔记(六) dimensionality-reduction-with-pca

    # 用PCA降维 #计算协方差矩阵 import numpy as np X=[[2,0,-1.4], [2.2,0.2,-1.5], [2.4,0.1,-1], [1.9,0,-1.2]] np.c ...

  9. 神器 Tmux 的超绝便利

    服务器的任务不间断运行,就是利用了 tmux 的特性.就是说,一般 ssh 是断开就会停止所有之前连接 ssh 期间运行的所有 processes,而 tmux 的核心业务不在于把屏幕分成几块好看,而 ...

  10. 关于恶意说说自动在QQ空间转发的机制

    有些很讨厌的带链接说说,只要你在手机打开它,就会自动转发,内容极其不雅 一怒之下我决定看个究竟首先,在此页开头有此关键语句: <iframe src="http://rtb.map.q ...