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 };

 

posted on 2015-03-21 17:12  keepshuatishuati  阅读(141)  评论(0编辑  收藏  举报