partitionList<T extends Comparable> function
Implementation
LinkedListNode<T>? partitionList<T extends Comparable>(
LinkedListNode<T>? head,
T x,
) {
if (head == null) return null;
final beforeHead = LinkedListNode<T>(x);
final afterHead = LinkedListNode<T>(x);
var before = beforeHead;
var after = afterHead;
LinkedListNode<T>? current = head;
while (current != null) {
if (current.value.compareTo(x) < 0) {
before.next = LinkedListNode<T>(current.value);
before = before.next!;
} else {
after.next = LinkedListNode<T>(current.value);
after = after.next!;
}
current = current.next;
}
// If no elements were less than x, return the after list
if (beforeHead.next == null) return afterHead.next;
before.next = afterHead.next;
return beforeHead.next;
}