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 ...
随机推荐
- "sessionFactory " or "hibernateTemplate " is required异常
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/ ...
- GNU INET SOCKET
Linux程序设计入门 - socket/inetd programming UNIX Socket Programming基本上是一本书名.Socket programming其实需要相 当程度的基 ...
- ExtJS简单的动画效果(ext js淡入淡出特效)
1.html页面:Application HTML file - index.html <html> <head> <title>ExtJs fadeIn() an ...
- IE7和IE8出现的计算判断问题
吸住底部菜单 IE7和IE8下会卡死的算法 ; } function fixedBar(){ var _height=$(this).height()+$(this).scrollTop(); var ...
- C语言宏定义使用技巧
写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1.防止一个头文件被重复包含 #ifndef COMDEF_ ...
- SQL Server判断对象是否存在 (if exists (select * from sysobjects )(转)
1 判断数据库是否存在Sql代码 if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名] ...
- C++引用变量学习
版权所有,转载请注明来源 (1)reference variable(rv) 主要用处是作为方程的形式参数,使用rv 可以直接对原数据进行操作而不是该数据的拷贝,节省了时间和空间,尤其是对于结构体以及 ...
- hdu 4619 二分图最大匹配 ——最大独立集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 #include <cstdio> #include <cmath> # ...
- 高效的DDoS攻击探测与分析工具——FastNetMon
一.简介 FastNetMon这是一个基于多种抓包引擎(NetFlow, IPFIX, sFLOW, netmap, PF_RING, PCAP)的DoS/DDoS攻击高效分析工具,可以探测和分析网络 ...
- 【protobuf进阶】读取proto实体里的extensionObject对象的方法
//设置扩展对象 ProtoBuf.Extensible.AppendValue //读取扩展对象 ProtoBuf.Extensible.GetValue 最近通过C#的TcpClient调用jav ...