LeetCode Partition List

//  44ms
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 // Start typing your C/C++ solution below 13 // DO NOT write int main() function 14 ListNode a(0),b(0); 15 ListNode* heada,*taila ,*headb,*tailb ; 16 heada=taila=&a; 17 headb=tailb=&b; 18 ListNode *p,*r; 19 p=head; 20 while(p) 21 { 22 if(p->val<x) 23 { 24 taila->next=p; 25 p=p->next; 26 taila=taila->next; 27 } 28 else 29 { 30 tailb->next=p; 31 p=p->next; 32 tailb=tailb->next; 33 } 34 } 35 tailb->next=NULL; 36 taila->next=headb->next; 37 return heada->next; 38 } 39 };
原文地址:https://www.cnblogs.com/mengqingzhong/p/3118503.html