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.

&nbsp;
Example 1:

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


Example 2:

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


&nbsp;
Constraints:


	The number of nodes in the list is in the range [0, 300].
	-100 &lt;= Node.val &lt;= 100
	The list is guaranteed to be sorted in ascending order.


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None:
            return head
        cur=head.next
        prev=head
        while cur is not None:
            if cur.val==prev.val:
                prev.next=cur.next
            else:
                prev=cur
            cur=cur.next
        return head
class Solution {
    public ListNode deleteDuplicates(ListNode head) {

        if (head == null) {
            return head;
        }

        ListNode result = head;

        while (result != null) {
            if (result.next == null) {
                break;
            }

            if (result.val == result.next.val) {
                result.next = result.next.next;
            } else {
                result = result.next;
            }
        }

        return head;
    }
}
class Solution {
public:
    // Recursive Approach
    ListNode* deleteDuplicates(ListNode* head){
        // base case
        if(head==NULL || head->next==NULL)
            return head;
        // 1-1-2-3-3
        //we are giving next pointer to recursion and telling it to get it done for me
        ListNode* newNode=deleteDuplicates(head->next); // 1-2-3-3
        // after recursion we will get-- 1-2-3
        
        //now we will compare the head node with the newNode 
        // if both are same then return the newNode
        //else return the current head
        if(head->val==newNode->val) 
            return newNode;
        else{
            head->next=newNode;
            return head;
        } 
    }
};


class Solution {
public:
    // Iterative Approach
    ListNode* deleteDuplicates(ListNode* head){
        if(head==NULL || head->next==NULL) return head;
        
        ListNode* temp =head;
        while(temp->next!=NULL){
            // if the 2 consecutive nodes are equal then just delete the in between
            if(temp->val==temp->next->val){
                temp->next=temp->next->next;
                //dont need to update the temp variable as there can be more than 2 duplicates
                // 1-1-1-1-2-3-4-4-NULL
            }
            else{
                temp=temp->next; //update the temp variable
            }
        }
        return head;
    }
};
var deleteDuplicates = function(head) {
    // Special case...
    if(head == null || head.next == null)
        return head;
    // Initialize a pointer curr with the address of head node...
    let curr = head;
    // Traverse all element through a while loop if curr node and the next node of curr node are present...
    while( curr != null && curr.next != null){
        // If the value of curr is equal to the value of prev...
        // It means the value is present in the linked list...
        if(curr.val == curr.next.val){
            // Hence we do not need to include curr again in the linked list...
            // So we increment the value of curr...
            curr.next = curr.next.next;
        }
        // Otherwise, we increment the curr pointer...
        else{
            curr = curr.next; 
        }
    }
    return head;        // Return the sorted linked list without any duplicate element...
};
Remove Duplicates from Sorted List