给出一个链表和一个值x ,以x为参照将链表划分成两部分,使所有小于x的节点都位于大于或等于x的节点之前。
两个部分之内的节点之间要保持的原始相对顺序。
例如:
给出1→4→3→2→5→2 和 x=3,
返回1→2→2→4→3→5.
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ function partition( head , x ) { // write code here if(head==null || head.next==null || x==0){return head} var current = head var lowList = {} var lowCur = lowList var highList = {} var highCur = highList while(current){ if(current.val < x){ lowCur.next = current lowCur = lowCur.next }else{ highCur.next = current highCur = highCur.next } current = current.next } highCur.next = null lowCur.next = highList.next return lowList.next } module.exports = { partition : partition };