Skip to main content

Remove Duplicates from Sorted List

· One min read
Sandeep Bhardwaj

Remove Duplicates from Sorted List

/**
* 83. Remove Duplicates from Sorted List
* <p>
* Given a sorted linked list, delete all duplicates such that each element appear only once.
* <p>
* Example 1:
* <p>
* Input: 1->1->2
* Output: 1->2
* Example 2:
* <p>
* Input: 1->1->2->3->3
* Output: 1->2->3
*/
public class RemoveDuplicatesFromSortedList {
public ListNode deleteDuplicates(ListNode head) {
//base case
if (head == null || head.next == null)
return head;

ListNode current = head;
while (current != null) {
if (current.next == null)
break;

if (current.val == current.next.val) {
//removing duplicate breaking the link
current.next = current.next.next;
} else {
current = current.next;
}

}
return head;
}
}