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.
public ListNode deleteDuplicates(ListNode head) {
ListNode resultHead = new ListNode(Integer.MAX_VALUE);
ListNode resultEnd = resultHead;
while(head != null){
if(head.next == null || head.next.val != head.val){
resultEnd.next = head;
head = head.next;
resultEnd = resultEnd.next;
resultEnd.next = null;
}
else{
while(head.next != null && head.val == head.next.val)
head = head.next;
}
}
return resultHead.next;
}
上一题就删掉一行就行