← All problemsExample 1 Example 2
Problem
Reverse Linked List
Easy- linked-list
Turn a singly linked list back to front, so that the last node becomes the first, and return the new head.
Lists in this problem are written as a plain array of values, in order from the head.
- Input
head = [7, 9, 4, 2]- Output
[2, 4, 9, 7]- Explanation
the last node becomes the head and every
nextlink points the other way.
- Input
head = []- Output
[]- Explanation
an empty list has no nodes to turn around.
Constraints:
0 <= number of nodes <= 500-1000 <= node value <= 1000
Follow-up: can you do it by relinking the existing nodes, using O(1) extra space?
Tab indents. Press Esc, then Tab to leave the editor.
Ctrl or ⌘ + Enter runs the tests.
Run your code to see every test here. Nothing is submitted or recorded.