/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @param {number} x * @return {ListNode} */ var partition = function (head, x) { const d1 = new ListNode(); const d2 = new ListNode(); let t1 = d1, t2 = d2; while (head) { if (head.val < x) { t1.next = head; t1 = t1.next; } else { t2.next = head; t2 = t2.next; } head = head.next; } t1.next = d2.next; t2.next = null; return d1.next; };