📘
Reverse Linked List
EasyLinked 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: 1→2→3→4→5
- The linked list is reversed by updating the pointers of each node to point to the previous node: 5→4→3→2→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
Python 3.13.1
Test Results
0/0Run code to see test results.