Interview Bootcamp
Vote

0% completed

Middle of the LinkedList (easy)

Problem Statement

Given the head of a Singly LinkedList, write a method to return the middle node of the LinkedList.

If the total number of nodes in the LinkedList is even, return the second middle node.

Example 1:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> null
Output: 3

Example 2:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Output: 4

Example 3:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
Output: 4

Constraints:

  • The number of nodes in the list is in the range [1, 100].
  • 1 <= Node.val <= 100

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anthony DiFede

· 4 years ago

One thing that is really important about this solution is that, checking fast != null must come first because if fast is null, we cannot check fast.next (or we get an exception).

Adam Tadros

Adam Tadros

· 4 days ago

Question: This solution assumes no loops. What in the problem indicates there won't be loops?

Is it: " The number of nodes in the list is in the range [1, 100]"?