Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->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 cur = head;
while (cur.next != null) {
if (cur.val == cur.next.val) {
// delete the next node
cur.next = cur.next.next;
} else {
// move forward
cur = cur.next;
}
}
return head;
}
}

[LC] 83. Remove Duplicates from Sorted List的更多相关文章

  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. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  3. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

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

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  5. 【一天一道LeetCode】#83. Remove Duplicates from Sorted List

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

  6. 【LeetCode】83 - Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

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

  8. 83. Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  9. 【LeetCode】83 - Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

随机推荐

  1. Idea Spring工程不识别注解

    如图所示 Idea工具报出很多注解不识别,开始怀疑是 工具问题,重装Idea.配置lombak都不行,切换分支发现正常,一定是合入代码修改啥了,一行行比对,果然是这行 import org.sprin ...

  2. cafe-ssd數據集訓練

    训练方式::https://blog.csdn.net/xiao_lxl/article/details/79106837 caffe-ssd训练自己的数据集 https://blog.csdn.ne ...

  3. 29. docker swarm 创建 三个节点 swarm 的集群

    1.使用 vagrant 部署 三台 centos/7 的 环境 ###Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.req ...

  4. Python内置文件

    概述 为了提升效率,Python有些内置文件如 __pycache__.py 详解 1)__pycache__.py, python程序运行时不需要编译成二进制代码,而直接从源码运行程序 Python ...

  5. 函数返回值return

    #函数后面如果没有return系统会默认return none def ff(): print("打印return") return 15 # 函数在执行中遇到return就会停止 ...

  6. Tensorflow学习教程------实现lenet并且进行二分类

    #coding:utf-8 import tensorflow as tf import os def read_and_decode(filename): #根据文件名生成一个队列 filename ...

  7. python-day6爬虫基础之会话、Cookies、代理

    由于前几天看电脑时间过长,在昨天的时候,两个眼睛就有点疼痛感觉,所以昨天晚上就没有学习,博客也没有跟着写,今早去校医院买了点药,上午把老师要求的电路板画完了,已经发出去做了,现在闲来无事,看了一会关于 ...

  8. Python的IDE之Pycharm的使用

    Python的IDE之Pycharm的使用 一.为什么用IDE(Python集成开发环境-Pycharm) 到现在为止,我们也是写过代码的人啦,但你有没有发现,每次写代码要新建文件.写完保存时还要选择 ...

  9. Vue2生命周期

    这是Vue文档里关于实例生命周期的解释图 那么下面我们来进行测试一下 <section id="app-8"> {{data}} </section> va ...

  10. python画图例子代码

    matplotlib包,使得python可以使用类似matlab的命令 双坐标,子图例子 fig, axes = plt.subplots( 2,1, figsize=(14, 14) ) ax = ...