标签:
I am lazy so I did not clear the two dynamic allowcated .
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *partition(ListNode *head, int x) { 12 if (!head || !head->next) return head; 13 ListNode *ahead = new ListNode(0); 14 ListNode *bhead = new ListNode(0); 15 ListNode *atail = ahead, *btail = bhead; 16 while (head) { 17 if (head->val < x) { 18 atail->next = head; 19 atail = atail->next; 20 } else { 21 btail->next = head; 22 btail = btail->next; 23 } 24 head = head->next; 25 } 26 atail->next = bhead->next; 27 btail->next = NULL; 28 return ahead->next; 29 } 30 };
LeetCode - Refresh - Partition List
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/4355868.html