304 North Cardinal St.
Dorchester Center, MA 02124

# Remove Duplicates from Sorted List II LeetCode Solution

## Problem – Remove Duplicates from Sorted List II LeetCode Solution

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:

``````Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
``````

Example 2:

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

Constraints:

• The number of nodes in the list is in the range `[0, 300]`.
• `-100 <= Node.val <= 100`
• The list is guaranteed to be sorted in ascending order.

### Remove Duplicates from Sorted List II LeetCode Solution in Java

``````public ListNode deleteDuplicates(ListNode head) {
while(cur!=null){
while(cur.next!=null&&cur.val==cur.next.val){
cur=cur.next;
}
if(pre.next==cur){
pre=pre.next;
}
else{
pre.next=cur.next;
}
cur=cur.next;
}
}
``````

### Remove Duplicates from Sorted List II LeetCode Solution in Python

``````def deleteDuplicates(self, head):
dummy = pre = ListNode(0)
else:
pre = pre.next
return dummy.next
``````

### Remove Duplicates from Sorted List II LeetCode Solution in C++

``````class Solution {
public:

if (p->val != val) {
} else {
while (p && p->val == val) p = p->next;
return deleteDuplicates(p);
}
}
};
``````
##### Remove Duplicates from Sorted List II LeetCode Solution Review:

In our experience, we suggest you solve this Remove Duplicates from Sorted List II LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Remove Duplicates from Sorted List II LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Remove Duplicates from Sorted List II LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions