题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
ListNode node = head;
ListNode newHead = new ListNode(-1);
newHead.next = head;
ListNode pre = newHead;
while(node != null){
if(node.next == null || node.val != node.next.val){
pre = pre.next;
node = node.next;
}else{
while(node.next != null && node.val == node.next.val){
node = node.next;
}
pre.next = node.next;
node = node.next;
}
}
return newHead.next;
}
}
