Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

83. Remove Duplicates from Sorted List 🌟

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.

O(N) Time and O(1) Space

Code

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head)return head;
        ListNode *curr=head;

        while(curr->next!=NULL){
            if(curr->val==curr->next->val){
                curr->next=curr->next->next;
            }else{
                curr=curr->next;
            }
        }
        return head;
    }
};