Remove Duplicates from Sorted List
2015-07-29 12:18
267 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
移除有序链表里面的重复元素,2个指针来移动判断即可。
代码如下:
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
移除有序链表里面的重复元素,2个指针来移动判断即可。
代码如下:
/** * 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) return head; ListNode cur = head; ListNode after = head; while(after!=null){ after = after.next; while(after !=null && after.val==cur.val){ after = after.next; } cur.next = after; cur = after; } return head; } }
相关文章推荐
- leetcode-Merge Two Sorted Lists
- 深入掌握JMS(八):JMSReplyTo
- Android 讲义第二版 ViewSwitcher + GestureDetector 练习
- 使用 jQuery.i18n.properties 实现 Web 前端的国际化
- 关于差分约束系统的建图方法
- Jquery结合datagrid框架
- 关于java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor异常
- HDU 5319 Painter
- swift之函数式编程(二)
- js Unicode编码转换
- 深入掌握JMS(七):DeliveryMode例子
- JVM以及GC
- C++ sort(),stable_sort()和partial_sort()
- HTTP Header
- AWS EC2 调整云主机根卷大小
- AndroidStudio的一些使用问题
- php检查字符串中是否有外链的方法
- dp poj1948 Triangular Pastures
- Codeforces Round #285 (Div. 2) C. Misha and Forest 构造
- 项目中用户的资源文件存放的规范配置文件