206. Reverse Linked List
Description
Difficulty: Easy
Related Topics: Linked List, Recursion
Given the head
of a singly linked list, reverse the list, and return the reversed list.
Example 1:
1 | Input: head = [1,2,3,4,5] |
Example 2:
1 | Input: head = [1,2] |
Example 3:
1 | Input: head = [] |
Constraints:
- The number of nodes in the list is the range
[0, 5000]
. -5000 <= Node.val <= 5000
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
Hints/Notes
- 2023/08/02
- For recursive solution, only think how to handle then end case and how to handle a new tail
- 0x3F’s solution(checked)
1 | class Solution { |
Solution
Iterative solution:
1 | /** |
Language: C++
1 | /** |