83. Remove Duplicates from Sorted List

Easy

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

Example 1:

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

Example 2:

  1. Input: 1->1->2->3->3
  2. Output: 1->2->3
  1. package leetcode.easy;
  2.  
  3. public class RemoveDuplicatesFromSortedList {
  4. @org.junit.Test
  5. public void test() {
  6. ListNode l11 = new ListNode(1);
  7. ListNode l12 = new ListNode(1);
  8. ListNode l13 = new ListNode(2);
  9. l11.next = l12;
  10. l12.next = l13;
  11. l13.next = null;
  12. print(l11);
  13.  
  14. ListNode l21 = new ListNode(1);
  15. ListNode l22 = new ListNode(1);
  16. ListNode l23 = new ListNode(2);
  17. ListNode l24 = new ListNode(3);
  18. ListNode l25 = new ListNode(3);
  19. l21.next = l22;
  20. l22.next = l23;
  21. l23.next = l24;
  22. l24.next = l25;
  23. l25.next = null;
  24. print(l21);
  25.  
  26. ListNode l1 = deleteDuplicates(l11);
  27. print(l1);
  28. ListNode l2 = deleteDuplicates(l21);
  29. print(l2);
  30. }
  31.  
  32. private static void print(ListNode l) {
  33. while (l != null) {
  34. System.out.print(l.val);
  35. if (l.next != null) {
  36. System.out.print("->");
  37. }
  38. l = l.next;
  39. }
  40. System.out.println();
  41. }
  42.  
  43. public ListNode deleteDuplicates(ListNode head) {
  44. ListNode current = head;
  45. while (current != null && current.next != null) {
  46. if (current.next.val == current.val) {
  47. current.next = current.next.next;
  48. } else {
  49. current = current.next;
  50. }
  51. }
  52. return head;
  53. }
  54. }

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. idea中Git配置,Git的非命令操作

    1.更换Git账户 在idea中File-->Settings-->Appearance-->System Settings-->Passwords,选择不保存密码(Do no ...

  2. java中创建对象的方式

    Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 使用new关键字 } → 调用了构造函数 使用Class类的newInstance方法 } → 调用了构造函数 使用Construc ...

  3. js闭包随记

    理解闭包可以将以上代码分解如下: function outerFunction() {     ;     function innerFunction(){         return count ...

  4. Python json常用操作

    json模块 (字符串操作) json.dumps() :对数据进行编码 json.loads() :对数据进行解码 json模块(文件操作) # 写入 JSON 数据 with open('data ...

  5. 使用openoffice转pdf,详细

    期由于项目的需求,需要word文档转pdf在线预览,由于一直没有接触这块,所以花了将近四天时间才弄明白. 写这篇文章的主要目的是加深自己的记忆,同时方便以后在用. (最近有使用了这个功能,发现这篇文章 ...

  6. 服务器nginx配置显示文件而不是下载

    有时候在服务器上配置某些文件比如TXT文件,在浏览器打开的时候,会弹出下载.如何只让他在浏览器中显示,而不是下载呢.在nginx配置文件中添加一行代码 add_header Content-Type ...

  7. Spring security invalid-session-url 的坑(配了permitAll仍然跳转到登录页)

    Spring security session配置中如果配了如下的invalid-session-url,配置了permitAll链接首次链接系统时会跳转到登录页,将该配置删除即可解决此问题. < ...

  8. MongoDB 3.4 功能改进一览

    MongoDB 3.4 已经发布,本文主要介绍 3.4 版本在功能特性上做的改进,内容翻译自 [https://docs.mongodb.com/manual/release-notes/3.4/?_ ...

  9. Map集合类

    1.1 Map.clear方法——从Map集合中移除所有映射关系 public static void main(String[] args) {    Map map=new HashMap();  ...

  10. MySQL5.5安装步骤

    原文链接:https://blog.csdn.net/m0_38025207/article/details/80723095 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类 ...