Remove Duplicates from Sorted List II LeetCode Solution

Last updated on October 9th, 2024 at 10:34 pm

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

Topics

Linked List

Level of Question

Medium

Remove Duplicates from Sorted List II LeetCode Solution

Remove Duplicates from Sorted List II 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:

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]

1. Remove Duplicates from Sorted List II Leetcode Solution C++

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head||!head->next) return head;
        ListNode* dummy = new ListNode(0);
        ListNode* tail = dummy;
        int flag = true; // should the current head be added ?
        while(head){
            while(head&&head->next&&head->val==head->next->val)
            {
                flag = false; // finds duplicate, set it to false
                head = head->next;
            }
            if(flag) // if should be added
            {
                tail->next = head;
                tail = tail->next;
            }
            head = head->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;        
    }
};

2. Remove Duplicates from Sorted List II Leetcode Solution Java

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
    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;        
    }
}

3. Remove Duplicates from Sorted List II Leetcode Solution JavaScript

var deleteDuplicates = function(head) {
    if (head == null || head.next == null)
        return head;
    var fake = new ListNode(0);
    fake.next = head;
    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;      
};

4. Remove Duplicates from Sorted List II Solution Python

class Solution(object):
    def deleteDuplicates(self, head):
        fake = ListNode(-1)
        fake.next = head
        curr, prev = head, fake
        while curr:
            while curr.next and curr.val == curr.next.val:
                curr = curr.next
            if prev.next == curr:
                prev = prev.next
                curr = curr.next
            else:
                prev.next = curr.next
                curr = prev.next
        return fake.next
Scroll to Top