Middle of a Linked List

Linked List construction is covered here

Example

Linked List: 1->2->3->4->5
Middle: 3
Linked List: 1->2->3->4
Middle: 2

Solution:

var middle = function (listHead) {
  var slow = listHead
  var fast = listHead

  while (fast != null && fast.next != null) {
    slow = slow.next
    fast = fast.next
    if (fast.next == null) {
      break
    }
    fast = fast.next
  }

  return slow
}

Written by Gagandeep Rangi who likes to talk about himself in third person. Twitter Instagram