There is a task to find a middle of Linked List. Could you please explain why does fast.next
in while()
loop throw a “TypeError: Cannot read properties of null (reading ‘next’)”?
const middleNode = (head) => {
let fast = head
let slow = head
if (head !== null) {
while (fast.next !== null) {
fast = fast.next.next
slow = slow.next
}
}
return slow
}