【leetcode】【82】Remove Duplicates from Sorted List II
2016-03-03 11:02
337 查看
一、问题描述
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
二、问题分析
首先head可能会被delete,所以最好额外添加一个头结点。剩下的无非就是链表的遍历问题。比较简单。三、Java AC代码
public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.next != null && cur.val == cur.next.val) {
while (cur.next != null && cur.val == cur.next.val)
cur = cur.next;
} else {
pre.next = cur;
pre = pre.next;
}
cur = cur.next;
}
pre.next = null;
return dummy.next;
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树