2.4链表 链表分割

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) : val(x), next(NULL) {}
 6 };*/
 7 class Partition {
 8 public:
 9     ListNode* partition(ListNode* pHead, int x) {
10         // write code here
11         ListNode* small = new ListNode(0);
12         ListNode* big = new ListNode(0);
13         ListNode* smallDummy = small;    
14         ListNode* bigDummy = big;    
15         while (pHead)
16         {
17             if (pHead->val < x)
18             {
19                 small->next = pHead;
20                 small = small->next;
21             }
22             else
23             {
24                 big->next = pHead;
25                 big = big->next;
26             }
27             pHead = pHead->next;
28         }
29         big->next = NULL;
30         small->next = bigDummy->next;
31         return smallDummy->next;
32     }
33 };
posted @ 2016-07-26 16:07  Pearl_zju  阅读(230)  评论(0编辑  收藏  举报