Remove Duplicates from Sorted List(链表)
2016-02-16 21:31
357 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
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) { ListNode p=head; //这样是跟着动的 while(p!=null){ while(p!=null && p.next!=null && p.val==p.next.val){ p.next=p.next.next; } p=p.next; } return head; } }
相关文章推荐
- 20160205.CCPP体系详解(0015天)
- Static table views are only valid when embedded in UITableViewController instances
- LeetCode45. Jump Game II
- LeetCode Algorithms #263 <Ugly Number>
- 移动web开发框架研究
- Android 性能优化之使用MAT分析内存泄露问题
- 2016开始记录自己程序员生涯的所见所得
- FileInputStream和FileOutputStream的使用
- (p124)k分位数
- c语言入门之项目1.5——字符数据的输入
- windows多线程同步机制---原子锁
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
- Button ToggleButton Spinner Adapter Inflate
- 关于字符型数组存放宽度问题
- C++多态性
- 面板组件-盒布局
- 浅析计算机接口技术
- Codeforces 626A Robot Sequence
- 什么是引力波
- 十大编程算法助程序员走上高手之路