LeetCode 86. Partition List
1 class Solution { 2 public: 3 ListNode* partition(ListNode* head, int x) { 4 ListNode* less = new ListNode(0), * greater = new ListNode(0); 5 ListNode* lessH = less, * greaterH = greater; 6 while(head){ 7 if(head->val < x){ 8 less->next = head; 9 less = less->next; 10 } 11 else{ 12 greater->next = head; 13 greater = greater->next; 14 } 15 head = head->next; 16 } 17 less->next = greaterH->next; 18 greater->next = NULL; 19 return lessH->next; 20 } 21 };