首页 > 其他 > 详细

LeetCode - Refresh - Partition List

时间:2015-03-21 18:31:05      阅读:372      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!