PIXELBANKv8.2.1
Menu

Reverse Linked List

Given a linked list (as comma-separated values), reverse it and return the reversed list.

Output as space-separated values.

Example:

Input:
1,2,3,4,5
Output:
5 4 3 2 1
Reasoning:
  • The input string "1,2,3,4,5" is split into individual node values: 1, 2, 3, 4, 5
  • A linked list is created from these values, with each node pointing to the next one: 123451 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5
  • The linked list is reversed by updating the pointers of each node to point to the previous node: 543215 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 1
  • The reversed linked list is then traversed to extract the node values in the reversed order: 5, 4, 3, 2, 1
  • The final output is the reversed list as space-separated values: 5 4 3 2 1

Constraints:

  • 0 <= number of nodes <= 5000
  • -5000 <= Node.val <= 5000
Editor

Test Results

0/0
Run code to see test results.