第一题:遍历链表,遇到重复节点就连接到下一个。

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

第二题:思路比较简单,设置一个超前节点作为head的前节点,往下遍历,遇到重复的就把超前节点连接到新的val节点。

public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null) return head;
ListNode res = new ListNode(0);
res.next = head;
ListNode list = res;
while (res.next!=null&&res.next.next!=null)
{
ListNode temp = res.next.next;
if (res.next.val==res.next.next.val)
{
while (temp.next!=null&&temp.next.val==temp.val)
{
temp = temp.next;
}
if (temp.next!=null) res.next = temp.next;
else res.next = null;
}
else res =res.next;
}
return list.next;
}

当要经常删除第一个节点是,要设置一个超前节点

[leetcode]82. Remove Duplicates from Sorted List的更多相关文章

  1. [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. linux系统下oracle表空间占用情况

    1.我们先查询表空间的占用情况,使用sql如下: select upper(f.tablespace_name) "表空间名", d.tot_grootte_mb "表空 ...

  2. 思维导图学《JVM 虚拟机规范》

    目录 工具 虚拟机实现 class 文件结构 字节码指令 其他 虚拟机结构 公众号 coding 笔记.点滴记录,以后的文章也会同步到公众号(Coding Insight)中,希望大家关注_ 公众号 ...

  3. uniapp cli版本中如何引入scss?

    一.安装依赖 npm i node-sass@4.14.1 sass-loader -D 二.在脚手架版本新建项目成功后,官方为我们准备了uni.scss文件,在这个里面写即可全局使用. ... 一. ...

  4. python大数问题

    python不需要特殊的声明,可以直接进行大数运算 验证:

  5. MAT内存分析工具安装指南(MAT)

    https://blog.csdn.net/mahl1990/article/details/79298616

  6. 解决 Zuul 中 OAuth2 报 unauthorized 错误

    问题描述 微服务中使用 OAuth2 鉴权,直接访问正常,通过 Zuul 访问报错: { "error": "unauthorized", "erro ...

  7. docker 部署 mongodb 并且开启远程连接

    mongodb 使用 docker 部署 mongodb 拉取镜像 docker pull mongo 可以查看镜像是否下载成功 docker images | grep mongo 应该会有如下的显 ...

  8. python核心高级学习总结1---------*args和**kwargs

    *args 和 ** kwargs 的用法 首先,这两者在用法上都是用来补充python中对不定参数的接受. 比如下面的列子 def wrappedfunc(*args, **kwargs): pri ...

  9. 图像处理术语解释:灰度、色相、饱和度、亮度、明度、阿尔法通道、HSL、HSV、RGBA、ARGB和PRGBA以及Premultiplied Alpha(Alpha预乘)等基础概念详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 由于老猿以前没接触过图像处理,在阅读moviepy代码时,对类的有些处理方法代码看不懂是什么含义,为此花了4天时间查阅了大量资料,并加以自己的理解和 ...

  10. PyQt(Python+Qt)学习随笔:QDockWidget停靠窗toggleViewAction方法的作用

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 toggleViewAction方法返回一个动作对象,该动作对象通过点 ...