86.分隔链表

分隔链表

给你一个链表的头节点 head 和一个特定值 x,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。

解析

1
2
3
4
5
6
7
8
9
10
11
12
var partition = function (head, x) {
const small = new ListNode(0), large = new ListNode(0);
let s = small, l = large, curr = head;
while (curr) {
if (curr.val < x) { s.next = curr; s = s.next; }
else { l.next = curr; l = l.next; }
curr = curr.next;
}
l.next = null;
s.next = large.next;
return small.next;
};

86.分隔链表
https://leetcode.lz5z.com/86.partition-list/
作者
tickli
发布于
2024年1月7日
许可协议