删除有序链表中的重复节点
2016-04-21 17:12
375 查看
//表示对递归掌握的不是很熟,链表的操作好多都是递归实现!!!是时候好好学学递归了
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
============================================
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
}
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.
============================================
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
}
相关文章推荐
- 服务器端主动关闭连接, 产生的TIME_WAIT状态为什么会占用服务端大量端口?
- CreateThread()函数的简单程序
- 15级电气2班阳军作业
- 在多线程中使用静态方法是否有线程安全问题
- Table of Contents - Apache Commons
- Pie
- 常用的正则表达式
- Fresco 框架配置说明文档(更新中)
- 一篇很全面的freemarker教程
- 不用itunes帐号如何打ipa包
- 修改Mac命令提示符
- HDU 4417 Super Mario(划分树+二分)
- Android依赖注入:Dagger、RoboGuice和ButterKnife
- <LeetCode OJ> 343. Integer Break
- 四中启动模式和onNewIntent的关系
- 初学php(二)
- 配置freeglut和glew
- Git常用命令总结
- window客户端访问HDFS
- Android Stuido快捷键