Given a sorted linked list, delete all duplicates such that each element appear onlyonce.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
Subscribeto see which companies asked this question.
Hide Tags
public ListNode deleteDuplicates(ListNode head) {
ListNode root = head;
if(head == null) return root;
while(head.next != null){
ListNode next = head.next;
if(head.val == next.val){
head.next = next.next;
}else{
head = next;
}
}
return root;
}