Linked Lists · Coding problem

Middle of the Linked List

A free coding problem from the Two-Pointer List Traversal skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Return the middle node of a singly linked list. With an even number of nodes there are two middles; return the second one.

Input format

The first line is n, the number of nodes. The second line has n space-separated values.

Output

Print the values of the list starting from the middle node through the end, space-separated (this makes the answer checkable without needing to know the list's exact length up front).

Example

Input:
7
9 4 7 1 8 2 6
Output:
1 8 2 6

Solve it in one pass, rather than counting the length first and walking to the middle in a second pass.

Constraints

  • 1 <= n <= 100
  • 1 <= node value <= 100

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Two-Pointer List Traversal

Solve it Middle of the Linked List free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime