jayati

Remove Duplicates from Sorted List

Feb 27th, 2023
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     ListNode* deleteDuplicates(ListNode* head) {
  14.         ListNode *ans = new ListNode(0);
  15.         ListNode *curr = ans;
  16.  
  17.         while(head) {
  18.             while(head->next && head -> val == head -> next -> val) head = head -> next;
  19.             curr -> next = head;
  20.             curr = head;
  21.             head = head -> next;
  22.         }
  23.         return ans->next;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment