U

57. Reverse Linked List Iteratively

Easy

Problem Description

Reverse a singly linked list of NN integers.

Input Format

  • •Line 1: An integer NN (0≤N≤1050 \le N \le 10^5).
  • •Line 2: NN space-separated integers.

Output Format

Print the reversed list space-separated, or EMPTY if empty.

Example 1:
Input:
5
1 2 3 4 5
Output:
5 4 3 2 1
Explanation: Reversed list is 5 4 3 2 1.
Example 2:
Input:
1
42
Output:
42
Explanation: Single node reversed is 42.
Example 3:
Input:
0
Output:
EMPTY
Explanation: Empty list yields EMPTY.
Constraints:
  • Standard integer bounds: 32-bit signed integer or input range [1, 10^5].
  • Time complexity target: O(N) or O(N log N) where applicable.
  • Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online