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 .
1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7 * }8 */9 Public classSolution {Ten PublicListNode partition (ListNode head,intx) { One if(Head = =NULL)returnhead; AListNode Pre1 =NewListNode (0); -ListNode Pre2 =NewListNode (0); -ListNode L1 =Pre1; theListNode L2 =Pre2; - while(Head! =NULL){ - if(Head.val <x) { -L1.next =head; +L1 =L1.next; -}Else{ +L2.next =head; AL2 =L2.next; at } -Head =Head.next; - } -L2.next =NULL; -L1.next =Pre2.next; - returnPre1.next; in } -}
Partition List Java Solutions