Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Remove Duplicates from Sorted List II problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes