# Remove Duplicates from Sorted List II LeetCode Solution

Here, We see Remove Duplicates from Sorted List II LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

Given the `head` of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

```Example 1:

Output: [1,2,5]

Example 2:

Output: [2,3]
```

## Remove Duplicates from Sorted List II Leetcode Solution C++

``````class Solution {
public:
ListNode* dummy = new ListNode(0);
ListNode* tail = dummy;
int flag = true; // should the current head be added ?
{
flag = false; // finds duplicate, set it to false
}
if(flag) // if should be added
{
tail = tail->next;
}
flag = true; // time for a new head value, set flag back to true
}
tail->next = nullptr; // Don't forget this... I did..
return dummy->next;
}
};```Code language: PHP (php)```

## Remove Duplicates from Sorted List II Leetcode Solution Java

``````class Solution {
ListNode dummy = new ListNode(0), fast = head, slow = dummy;
slow.next = fast;
while(fast != null) {
while (fast.next != null && fast.val == fast.next.val) {
fast = fast.next;    //while loop to find the last node of the dups.
}
if (slow.next != fast) { //duplicates detected.
slow.next = fast.next; //remove the dups.
fast = slow.next;     //reposition the fast pointer.
} else { //no dup, move down both pointer.
slow = slow.next;
fast = fast.next;
}

}
return dummy.next;
}
}```Code language: PHP (php)```

## Remove Duplicates from Sorted List II Leetcode Solution JavaScript

``````var deleteDuplicates = function(head) {
var fake = new ListNode(0);
var curr = fake;
while(curr.next != null && curr.next.next != null){         // curr.next means the next node of curr pointer and curr.next.next means the next of next of curr pointer...
if(curr.next.val == curr.next.next.val) {
let duplicate = curr.next.val;
while(curr.next !=null && curr.next.val == duplicate) {
curr.next = curr.next.next;
}
}
else{
curr = curr.next;
}
}
return fake.next;
};```Code language: JavaScript (javascript)```

## Remove Duplicates from Sorted List II Solution Python

``````class Solution(object):
fake = ListNode(-1)