83. Remove Duplicates from Sorted List

Easy

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
package leetcode.easy;

public class RemoveDuplicatesFromSortedList {
@org.junit.Test
public void test() {
ListNode l11 = new ListNode(1);
ListNode l12 = new ListNode(1);
ListNode l13 = new ListNode(2);
l11.next = l12;
l12.next = l13;
l13.next = null;
print(l11); ListNode l21 = new ListNode(1);
ListNode l22 = new ListNode(1);
ListNode l23 = new ListNode(2);
ListNode l24 = new ListNode(3);
ListNode l25 = new ListNode(3);
l21.next = l22;
l22.next = l23;
l23.next = l24;
l24.next = l25;
l25.next = null;
print(l21); ListNode l1 = deleteDuplicates(l11);
print(l1);
ListNode l2 = deleteDuplicates(l21);
print(l2);
} private static void print(ListNode l) {
while (l != null) {
System.out.print(l.val);
if (l.next != null) {
System.out.print("->");
}
l = l.next;
}
System.out.println();
} public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while (current != null && current.next != null) {
if (current.next.val == current.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
}

LeetCode_83. Remove Duplicates from Sorted List的更多相关文章

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

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

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

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

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. 【CRT】中国剩余定理简介

    中国剩余定理(CRT) 中国剩余定理出自中国的某本古书,似乎是孙子兵法?(雾 其中有这样一个问题: 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 即,对于这样一个方程组: \[ ...

  2. python----PySnooper获取打印日志

    官网链接:https://pypi.org/project/PySnooper/ 安装:pip install PySnooper 使用方式,直接 导入import pysnooper,添加装饰器   ...

  3. Oracle-查看sql运行状况

    查看占io较大的正在运行的session SELECT se.sid, se.serial#, pr.SPID, se.username, se.status, se.terminal, se.pro ...

  4. 什么是EDID,EDID能做什么,EDID基本介绍

    转至 http://blog.csdn.net/Jkf40622/article/details/48311455 Q1: 为什么要写这篇文章? A1:在最近的工作中遇到了不少问题,其中很多都是和ED ...

  5. Spring 整合 Quartz框架(定时任务)

    Maven 无法下载 Quartz 依赖,去官网下载 http://www.quartz-scheduler.org/downloads/ Quartz 官方手册:https://www.w3csch ...

  6. Neo4j数据进行备份、还原

    一.neo4j备份方式 neo4j数据库的备份还原分为两种: offline 和 online. Offline backup - dump Dump a database into a single ...

  7. Convert AS400 Spool to PFD Tools – PDFing

    1. Steps There’s a tool PDFing convert spool file to PDF with simple way. No need install AS400 obje ...

  8. leetcode解题报告(19):Contains Duplicate II

    描述 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  9. [TJOI2013]松鼠聚会 曼哈顿距离

    [TJOI2013]松鼠聚会 luogu P3964 首先容易得到两点间距离是\(max(|x_1-x_2|, |y_1-y_2|)\)(即切比雪夫距离) 然后有个套路:原\((x,y)\)求曼哈顿距 ...

  10. mac 安装rabbitmq出现的问题

    一直提示这个文件权限问题. 然后试了各种办法.chown user:group / chmod 777 等等.都没有解决. 最后查到 chmod 666 /users/xxx/.erlang.cook ...