DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on • Edited on

Remove Duplicates from Sorted LinkedList

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Approach 1
pntr1 & pntr2 is only for understanding purpose

var deleteDuplicates = function(head) {
   let pntr1 = head;
  while (pntr1 && pntr1.next) {
    let one = pntr1;
    let two = pntr1.next;

    if (one.val === two.val) {
      pntr1.next = pntr1.next.next;
    } else {
      pntr1 = pntr1.next;
    }
  }
    return head;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)