码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode - Refresh - Partition List

时间:2015-03-21 18:33:56      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!