876 Middle of the Linked List

Problem Statement

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.
Input: head = [1,2,3,4,5]
Output: [3,4,5]

Approach

This can be solved with a fast and slow pointer approach. Once the fast pointer reaches the end of the linked list our slow pointer will be in the middle of the linked list.
Our while loop has to check for fast && fast.next as fast could possibly be undefined in the assignment.

Solution

var middleNode = function(head) {
  let fast = head
  let slow = head

  while (fast && fast.next) {
    slow = slow.next
    fast = fast.next.next
  }
  return slow
};

Questions

References

LC