leetcode修炼之路——83. Remove Duplicates from Sorted List
哈哈,我又来了。昨天发现题目太简单就没有放上来,今天来了一道有序链表的题。题目如下:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
题目分析:简单来说就是去除有序链表中重复的值。
代码实现:
ListNode:
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
具体代码实现:
public static ListNode deleteDuplicates(ListNode head) {
if (head != null) {
if (head.next != null) {
if (head.val == head.next.val) {//如果值相等
head.next = head.next.next;//让他后面的指向后面的后面
deleteDuplicates(head);//再次进行比较
} else {
deleteDuplicates(head.next);
}
}
}
return head;
}
总体来说还是比较简单,一次ac,哈哈。
leetcode修炼之路——83. Remove Duplicates from Sorted List的更多相关文章
- LeetCode Linked List Easy 83. Remove Duplicates from Sorted List
Description Given a sorted linked list, delete all duplicates such that each element appear only onc ...
- 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题要求 ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 【一天一道LeetCode】#83. Remove Duplicates from Sorted List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【Leetcode】【Medium】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 II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- optimize performance
http://www.cnblogs.com/fullhouse/archive/2012/01/05/2313105.html http://www.cnblogs.com/fullhouse/ar ...
- SQL Server 中的嵌套事务与@@TranCount(转)
在处理事务的时候,一般都用RollBack Transaction来回滚,但是如果在嵌套事务中这样使用的话,就会出现错误. 在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映 ...
- .net线程入门1-进程
什么是进程 当用户启动了一个程序,这个程序会加载内存和一大堆的资源,这些内存和资源在物理上的分区就是一个进程.一个应用程序也许不仅仅包含一个进程,了解程序和进程不是同一回事是非常重要的. 你可以通过任 ...
- 为什么PCI-e比SATA快这么多?
PCIe协议和SATA协议都是分层协议,分为物理层,数据链路层,传输层,命令层和应用层. 硬件工程师主要关注物理层.数据链路层和传输层.所有CMD/data由应用层和命令层打下来,每向下走一层,多一层 ...
- updatepanel刷新后重新加载js脚本问题
在页尾加 <script type="text/javascript"> Sys.WebForms.PageRequestManager.getInstance().a ...
- WPF之插件开发
一:解决方案管理器截图 效果图: 二:简单功能说明 IMsg定义了一个接口,MYPlugin1实现接口功能,”插件式开发“实现程序运行时再调用非本身引用的dll文件,调用该dll的方法实现功能 三:I ...
- Java---类反射(1)---类反射入门和基础
什么是类反射 ☆什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方 ...
- iOS Block中的weakSelf/strongSelf
Objective C 的 Block 是一个很实用的语法,特别是与GCD结合使用,可以很方便地实现并发.异步任务.但是,如果使用不当,Block 也会引起一些循环引用问题(retain cycle) ...
- 软工UML学习札记
UML模型由:事物.关系和图组成 (1)类(class)── 类用带有类名.属性和操作的矩形框来表示. (2)主动类(active class)── 主动类的实例应具有一个或多个进程或线程,能够启动控 ...
- docs
https://www.eucalyptus.com/docs/eucalyptus/3.4/index.html [Eucalyptus PDF官方下载] http://aws.amazon.co ...