Middle of the Linked List
EasyLinked ListTwo Pointers
Solution
import { ListNode } from '@algorithm/lib';
export function middleNode(head: ListNode | null): ListNode | null {
if (head === null) {
return null;
}
if (head.next === null) {
return head;
}
let [left, right]: Array<ListNode | null> = [head, head];
while (right && right.next) {
right = right?.next?.next ?? null;
left = left?.next ?? null;
}
return left;
}