title :
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greate R than or equal to x.
You should preserve the original relative order of the nodes in each of the.
For example,
Given 1->4->3->2->5->2 and x = 3,
Return 1->2->2->4->3->5 .
Code :
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Partition (listnode* head,intx) {ListNode dummyless (-1), Dummygreaterorequal (-1); ListNode*P1 = &dummyless, *p2 = &dummygreaterorequal, *p =Head; while(p) {if(P->val <x) {P1->next =p; P1= p1->Next; } Else{P2->next =p; P2= p2->Next; } P= p->Next; } p1->next =Dummygreaterorequal.next; P2->next =NULL; returnDummyless.next; }};
Tips:
The underlying operation of the linked list.
Set up two virtual table headers (representing two sub-linked lists), respectively, and then back to less than X and greater than or equal to X, and then connect the two sub-linked list.
"Partition List" cpp