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.
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;
head = head.next;
}
}
return resultHead.next;
}