nikunjsoni

203

Mar 16th, 2021
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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* removeElements(ListNode* head, int val) {
  14.         if(!head)
  15.             return head;
  16.         while(head && head->val == val)
  17.             head = head->next;
  18.         ListNode* curr=head;
  19.         while(curr && curr->next){
  20.             if(curr->next->val == val)
  21.                 curr->next = curr->next->next;
  22.             else
  23.                 curr = curr->next;
  24.         }
  25.         return head;
  26.     }
  27. };
RAW Paste Data