v9.1.1
Menu

Reverse Linked List

easyFREELinked ListBlind 75

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: 1→2→3→4→51 \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: 5→4→3→2→15 \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

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.