# Leetcode Remove Duplicates from Sorted List problem solution

Apr 25, 2023

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:

Output: [1,2]

Example 2:

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;
while(strobe->next!=NULL){
if(strobe->val==strobe->next->val){
strobe->next=strobe->next->next;
continue;
}
strobe=strobe->next;
}

}``````

## Solution in C++ Programming

``````/**
* 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:
while(temp and temp->next)
{
if(temp->val==temp->next->val)
temp->next=temp->next->next;
else
temp=temp->next;
}
}
};``````

## Solution in Java Programming

``````class Solution {

while(dummy != null && dummy.next != null){
if (dummy.val == dummy.next.val)
dummy.next = dummy.next.next;
else
dummy = dummy.next;
}
}
}``````

## Solution in Python Programming

``````class Solution:
ref = ListNode()
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

``````/**
* 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 {
while(currentNode != null && currentNode.next != null)
{
if(currentNode.val == currentNode.next.val)
currentNode.next = currentNode.next.next;
else
currentNode = currentNode.next;
}
}
}``````

#### 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.