Advertisement
nikunjsoni

86

Mar 21st, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 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* 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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement