Leetcode Remove Duplicates from Sorted List problem solution

In the Leetcode Remove Duplicates from Sorted List problem solution Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

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

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,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.

Solution in C Programming

struct ListNode* deleteDuplicates(struct ListNode* head){
    
    struct ListNode* strobe;    
    strobe=head;
    if(head==NULL){return NULL;}
    while(strobe->next!=NULL){
    if(strobe->val==strobe->next->val){
       strobe->next=strobe->next->next;
        continue;
    }
    strobe=strobe->next;
    }

    return head;
}

Solution in C++ Programming

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head){
        ListNode* temp=head;
        while(temp and temp->next)
        {
            if(temp->val==temp->next->val)
                temp->next=temp->next->next;
            else
                temp=temp->next;
        }
        return head;
    }
};

Solution in Java Programming

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = head;
        
        while(dummy != null && dummy.next != null){
            if (dummy.val == dummy.next.val)
                dummy.next = dummy.next.next;
            else
                dummy = dummy.next;
        }
        return head;
    }
}

Solution in Python Programming

class Solution:
    def deleteDuplicates(self, head):
        ref = ListNode()
        p1=head
        ref.next=p1
        while p1: 
            if p1.next: 
                if p1.next.val == p1.val: 
                    p1.next = p1.next.next
                else: 
                    p1=p1.next
            else: 
                p1 = p1.next    
        return ref.next

Solution in C# Programming

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode DeleteDuplicates(ListNode head) {
        var currentNode = head;
        while(currentNode != null && currentNode.next != null)
        {
            if(currentNode.val == currentNode.next.val)
                currentNode.next = currentNode.next.next;
            else
                currentNode = currentNode.next;
        }
        return head;
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

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