LeetCode_83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List
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的更多相关文章
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- idea中Git配置,Git的非命令操作
1.更换Git账户 在idea中File-->Settings-->Appearance-->System Settings-->Passwords,选择不保存密码(Do no ...
- java中创建对象的方式
Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 使用new关键字 } → 调用了构造函数 使用Class类的newInstance方法 } → 调用了构造函数 使用Construc ...
- js闭包随记
理解闭包可以将以上代码分解如下: function outerFunction() { ; function innerFunction(){ return count ...
- Python json常用操作
json模块 (字符串操作) json.dumps() :对数据进行编码 json.loads() :对数据进行解码 json模块(文件操作) # 写入 JSON 数据 with open('data ...
- 使用openoffice转pdf,详细
期由于项目的需求,需要word文档转pdf在线预览,由于一直没有接触这块,所以花了将近四天时间才弄明白. 写这篇文章的主要目的是加深自己的记忆,同时方便以后在用. (最近有使用了这个功能,发现这篇文章 ...
- 服务器nginx配置显示文件而不是下载
有时候在服务器上配置某些文件比如TXT文件,在浏览器打开的时候,会弹出下载.如何只让他在浏览器中显示,而不是下载呢.在nginx配置文件中添加一行代码 add_header Content-Type ...
- Spring security invalid-session-url 的坑(配了permitAll仍然跳转到登录页)
Spring security session配置中如果配了如下的invalid-session-url,配置了permitAll链接首次链接系统时会跳转到登录页,将该配置删除即可解决此问题. < ...
- MongoDB 3.4 功能改进一览
MongoDB 3.4 已经发布,本文主要介绍 3.4 版本在功能特性上做的改进,内容翻译自 [https://docs.mongodb.com/manual/release-notes/3.4/?_ ...
- Map集合类
1.1 Map.clear方法——从Map集合中移除所有映射关系 public static void main(String[] args) { Map map=new HashMap(); ...
- MySQL5.5安装步骤
原文链接:https://blog.csdn.net/m0_38025207/article/details/80723095 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类 ...