knakul853

Untitled

Jun 15th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     ListNode* partition(ListNode* head, int x) {
  7.        
  8.         ListNode* p1 = new ListNode();
  9.         ListNode* p2 = new ListNode();
  10.         ListNode* tempP1 = p1;
  11.         ListNode* tempP2 = p2;
  12.        
  13.         while( head )
  14.         {
  15.             if( head->val < x )
  16.             {
  17.                 p1->next = new ListNode( head->val );
  18.                 p1 = p1->next;
  19.             }
  20.             else{
  21.                 p2->next = new ListNode( head->val );
  22.                 p2 = p2->next;
  23.             }
  24.            
  25.             head = head->next;
  26.         }
  27.        
  28.         p2->next = NULL;
  29.         p1->next = tempP2->next;
  30.        
  31.         return tempP1->next;
  32.        
  33.     }
  34. };
Add Comment
Please, Sign In to add comment