SDE Path

Find the Middle of a Linked List

Easy

Find the Middle of a Linked List

Find the middle node of a singly linked list in a single pass using slow and fast pointers.

Input format

The first line contains N — the number of nodes. The second line contains the N node values in list order.

Output format

Print the value of the middle node. For an even number of nodes, print the second of the two middle nodes.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
5
1 2 3 4 5
Expected output
3
Example 2
Input
1
9
Expected output
9