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. ADS1.2 调试问题

    最近一个程序需要用到ADS1.2这个软件,在使用过程中出现了如下问题: 1.由于以前用的是KEIL,所以没找到文件的工程,查资料才发现,这个工程文件打开的文件是MCP格式的文件: 2.调试的时候,没找 ...

  2. elasticsearch-logstash

    1.logstash介绍 logstash 是ES 下的一款开源软件.用于数据采集,就是从Mysql等数据源采集数据.更新数据.然后将数据发送到ES中创建.更新索引 2.安装 演示环境是windows ...

  3. php速成_day3

    一.MySQL关系型数据库 1.什么是数据库 数据库 数据存储的仓库,在网站开发应用当中,需要有一些数据存储起来. 注册的用户信息,使用PHP变量只是一个临时的存储,如果需要永久的存储起来,就把数据存 ...

  4. delphi数据类型列表

    Delphi 数据类型列表 分类 范围 字节 备注 简单类型 序数 整数 Integer -2147483648 .. 2147483647 4 有符号32位 Cardinal 0 .. 429496 ...

  5. 对接memcache经验分享

    接口访问日志  数据结构 分享 apiname 接口名称 apiname[cnt]接口访问次数每访问一次增加一次 这里要处理并发问题 我还没有解决: apiname[cnt][n][spent_tim ...

  6. php URL各部分获取方法(全局变量)

    php URL各部分获取方法(全局变量),主要介绍php全局变量$_SERVER的用法,有需要的朋友,可以参考下. 1.$_SESSION['PHP_SELF'] - 获取当前正在执行脚本的文件名 2 ...

  7. NFS 文件共享

    备注:NFS 文件共享需设置两部分:服务端和客户端 一.服务端设置 1.1.查看nfs包是否安装,未安装则重新安装 [root@localhost ~]# rpm -qa|grep rpcbind r ...

  8. ZZJ_淘淘商城项目:day01(RESTful Web Service、SVN)

    淘淘商城项目是很适合初级Java程序员练习的实战项目,本次复习是另一位张老师教授的课,内容上与之前入老师版taotao商城比较有些新东西加了进来. 因此有必要记录下那些直到现在还可供参考的技术亮点分享 ...

  9. 绿洲作业第二周 - Y3每日中文学习任务清单

    1. 本周仍是古诗学习周,老师已在“最美诵读”上布置本周需完成的任务,请孩子在“最美诵读”小程序中,结合老师发的学习任务清单,合理安排时间进行学习.如果孩子另有学习安排,可在周日(2.23)23:59 ...

  10. Linux 进程信号量

    #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/ipc.h&g ...