LeetCode - Remove Duplicates from Sorted List
2016-01-03 13:05
162 查看
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
思路:
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.
思路:
package list; public class RemoveDuplicatesFromSortedList { public ListNode deleteDuplicates(ListNode head) { ListNode p = head; ListNode start = head; while (head != null && head.next != null) { ListNode headNext = head.next; if (head.next.val != head.val) { start = head.next; } else { start.next = head.next.next; } head = headNext; } return p; } public static void main(String[] args) { // TODO Auto-generated method stub ListNode a1 = new ListNode(1); ListNode a2 = new ListNode(1); ListNode a3 = new ListNode(1); ListNode a4 = new ListNode(1); ListNode a5 = new ListNode(1); a1.next = a2; a2.next = a3; a3.next = a4; a4.next = a5; a5.next = null; RemoveDuplicatesFromSortedList r = new RemoveDuplicatesFromSortedList(); ListNode head = r.deleteDuplicates(a1); while (head != null) { System.out.println(head.val); head = head.next; } } }
相关文章推荐
- [codeforces]Goodbye_2015
- 一些杂记
- scala 模式匹配详解 2 scala里是怎么实现的?
- 3.修改zabbix admin密码
- [Java开发之路](11)SAX解析XML文档
- Matlab lugui
- [No000078]Python3 字符串操作
- MySQL二进制包安装简略过程
- 支付宝 iOS SDK 的简单使用
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决Nginx+PHP-FPM出现502(Bad Gateway)错误问题
- iOS开发UI篇—程序启动原理和UIApplication
- Struts2学习第二章
- weighttp 使用
- Java中的类加载器
- 实例讲解避免javascript冲突的方法
- 从CRITS发展历史解读结构框架
- Min Stack 实现一个最小栈
- 黑马程序员-OC语言-Foundation-NSArray
- Android线程问题及主线程与子线程的交互