nikunjsoni

86

Mar 21st, 2021
79
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* partition(ListNode* head, int x) {
  14.         ListNode *dummy = new ListNode(0, head);
  15.         ListNode *partition = new ListNode(0);
  16.         ListNode *curr = dummy, *pcurr = partition;
  17.         while(curr->next){
  18.             if(curr->next->val < x){
  19.                 pcurr->next = curr->next;
  20.                 pcurr = pcurr->next;
  21.                 curr->next = curr->next->next;
  22.             }
  23.             else{
  24.                 curr = curr->next;
  25.             }
  26.         }
  27.         pcurr->next = dummy->next;
  28.         return partition->next;
  29.     }
  30. };
RAW Paste Data