Leetcode 83 Remove Duplicates from Sorted List Solution in c++ | Hindi Coding Community

0

 

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.


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

Output: [1,2,3]


class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *ans = new ListNode(0);
ListNode *curr = ans;

while(head) {
while(head->next && head -> val == head -> next -> val) head = head -> next;
curr -> next = head;
curr = head;
head = head -> next;
}
return ans->next;
}
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !