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. Topshelf 使用

    前言 在写后台代码的过程中,经常会遇到要写一些单独的服务.以前呢,直接用的是 .NET 下的 “Windows 服务” 控件开发的. 这个传统的控件开发起来很不方面,使用也不友好.发现有用 Topsh ...

  2. GET和POST的区别【转载】

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  3. Android测试-monkey

    好久以前搞过monkey,最近看了一个monkey+日志录制的一个分享,准备自己也搞一下. monkey的doc文档: https://developer.android.google.cn/stud ...

  4. 前端学习笔记--CSS3

    本本记录了css3的样式:浏览器支持度.圆角边框.阴影.文字与文本.过渡.动画.2d旋转.3d旋转 浏览器支持度: 1.圆角边框 例:只要确定了x.y值,就能知道弧度 画一个圆形:长=宽,border ...

  5. 多任务udp聊天器完整版

    import socket import threading def send_msg(udp_socket,dest_ip,dest_port): while True: send_data = i ...

  6. 【CSP模拟赛】God knows (李超线段树)

    题面 CODE 稍微分析一下,发现把(i,pi)(i,p_i)(i,pi​)看做二维数点,就是求极长上升子序列的权值最小值. 直接李超线段树 #include <bits/stdc++.h> ...

  7. js中的数据类型以及转换

    Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...

  8. 报警提示 System.NullReferenceException:“未将对象引用设置到对象的实例。

    System.NullReferenceException:“未将对象引用设置到对象的实例.是就因为Session在记录到服务器时,没有添加  IRequiresSessionState 所以运行时回 ...

  9. java 根据年月获取当前年月的最后一天,获取当前年月有多少天

    new Date( "2019-08-" + 1)   获取2019年8月的最后一天   结果为--2019-08-31 new Date(2019, 8, 0).getDate( ...

  10. java设计模式学习-单例模式

    java中单例模式定义:“一个类有且仅有一个实例,并且自行实例化向整个系统提供.”单例模式可以保证一个应用中有且只有一个实例,避免了资源的浪费和多个实例多次调用导致出错. 单例模式有以下特点: 1.单 ...