Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3Output: 1->2->2->4->3->5
思路: 遍历访问链表 head, 将链表中小于 x 与大于等于 x 的值作拆分成两个链表, 最后再将它们给链接起来。
/*** Definition for singly-linked list.* function ListNode(val) {* this.val = val;* this.next = null;* }*//*** @param {ListNode} head* @param {number} x* @return {ListNode}*/var partition = function(head, x) {const listNode = new ListNode(0)listNode.next = headconst smallerThanX = new ListNode(0)const biggerThanX = new ListNode(0)let cur = listNode.nextlet smallPoint = smallerThanXlet bigPoint = biggerThanXwhile (cur) {if (cur.val < x) {smallPoint.next = cursmallPoint = smallPoint.next} else {bigPoint.next = curbigPoint = bigPoint.next}cur = cur.next}bigPoint.next = nullsmallPoint.next = biggerThanX.nextreturn smallerThanX.next}