Reverse a linked list
Coding problemEasyAsked in: AMD
You are given the first node (the head) of a singly linked list (each node links only to the next one, not backwards). Each node holds an integer and a link to the next node; the last node links to nothing.
Reverse the list and return its new head. Change the links between the existing nodes; do not just print the values backwards. An empty list stays empty.
Examples
Example 1
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2
Input: head = [1,2]
Output: [2,1]
Example 3
Input: head = []
Output: []
Constraints
- The list has between 0 and 100,000 nodes
- Each value is a 32-bit integer
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.
to answer and keep your progress. Reading is free for everyone.