Topic:
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
.
Idea: Find the two-segment linked list that is divided by x, and connect it together, keeping a watch on the chain header.
Code:
Public classSolution { PublicListNode partition (ListNode head,intx) {ListNode small=NewListNode (0);//List of two new linksListNode big =NewListNode (0); ListNode smalltemp= Small;//Save the linked list headerListNode bigtemp =Big; while(head!=NULL){ if(Head.val <x) {Big.next=Head; Big=Big.next; }Else{Small.next=Head; Small=Small.next; } head=Head.next; } Big.next= Smalltemp.next;//ConnectionSmall.next =NULL; returnBigtemp.next; }}
[Leetcode-java] Partition List