Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Remove Duplicates from Sorted List II problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Remove Duplicates from Sorted List II problem solution, we have 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.

Leetcode Remove Duplicates from Sorted List II problem solution

Leetcode Remove Duplicates from Sorted List II problem solution in Python.

class Solution(object):
    def deleteDuplicates(self, head):
        if not head or not head.next :
            return head
        p = head
        while p.next and p.val == p.next.val:
            p = p.next
        if p != head:
            head = p.next
            return self.deleteDuplicates(head)
        head.next = self.deleteDuplicates(head.next)
        return head

Remove Duplicates from Sorted List II problem solution in Java.

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        
        ListNode pre = new ListNode(-1, head);
        ListNode curr = head;
        ListNode dummyHead = pre;
        
        while (curr != null && curr.next != null) {
            if (curr.val == curr.next.val) {
                while (curr != null && curr.next != null && curr.val == curr.next.val) {
                    curr = curr.next;
                }
                pre.next = curr.next;
            } else {
                pre = curr;
            }
            curr = curr.next;
        }
        
        return dummyHead.next;
    }
}

Problem solution in C++.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL) return head;
        ListNode *p0,*p1;
        p0 = new ListNode(0); /*It is convenient to create a node before head*/
        p0->next = head;
        p1 = p0;
        while(p1->next!=NULL){  /*remove the duplicates after and next to p1*/
            ListNode *tmp = p1->next;
            int current = tmp->val, count = 1;
            tmp = tmp->next;
            while(tmp != NULL && tmp->val == current){
                count++;
                tmp = tmp->next;
            }
            if(count > 1) p1->next = tmp;
            else p1 = p1->next;
        }
        return p0->next;
    }
};

Problem solution in C.

if(head == NULL)
    return NULL;
    
struct ListNode *p1,*p2;
p1 = head ; p2 = head;

while(p2 != NULL && p2->val == p1->val)
    p2 = p2->next;

if(p1->next == p2)
{
    p1->next = deleteDuplicates(p2);
    return p1;
}
else
    return deleteDuplicates(p2);

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes