Skills101
← All coding problems

Middle of the Linked List

easyLinked List+10 XPTime limit 2s

Find the middle node of a linked list and print the list from that node to the end. If there are two middle nodes (even length), use the second one.

List format. A linked list is given as two lines: its length n, then its n values from head to tail (the second line is empty when n = 0). When you print a list, print its values on one line separated by spaces, or the word empty if it has no nodes.

Input: one list (n ≥ 1).

Output: the values from the middle node to the tail.

Constraints: 1 ≤ n ≤ 10⁵.

Think about it: the slow and fast pointer trick — move slow one step and fast two steps; when fast reaches the end, slow is in the middle. One pass, no counting.

Sample input 1

5
1 2 3 4 5

Sample output 1

3 4 5

Sample input 2

6
1 2 3 4 5 6

Sample output 2

4 5 6

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.