Advertisement
nikunjsoni

82

Mar 19th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 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 *dummy = new ListNode(0, head);
  15.         ListNode *pre = dummy;
  16.         while(head){
  17.             if(head->next && head->val == head->next->val){
  18.                 while(head->next && head->val == head->next->val) head = head->next;
  19.                 pre->next = head->next;
  20.             }
  21.             else{
  22.                 pre = pre->next;
  23.             }
  24.             head = head->next;
  25.         }
  26.         return dummy->next;
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement