Reverse Linked List
LINKED LIST
RECURSION
Problem
Given the head of a singly linked list
, reverse the list and return the head of the reversed list.
Examples:
// 1 -> 2 -> 3 -> 4 -> 5 -> NULL reverseList([1,2,3,4,5]) // [5,4,3,2,1] // 5 -> 4 -> 3 -> 2 -> 1 -> NULL reverseList([3,4,5]) // returns [5,4,3] reverseList([1]) // returns [1]
Time Complexity
The time complexity of reversing a linked list is O(n)
, where n
is the number of nodes in the list. This can be achieved either iteratively or recursively.
Loading...
1
2
3
4
5