Reverse a singly linked list.
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
A linked list can be reversed either iteratively or recursively. Could you implement both?
-
Time complexity :
O(n)
. We traverse the linked-list containing n elements only once. The swap operation costsO(1)
time. -
Space complexity :
O(1)
. The extra space is only the one to store the result and the one temporary variable inside the loop.
-
Time complexity :
O(n)
. We traverse the linked-list containing n elements only once. The swap operation costsO(1)
time. -
Space complexity :
O(n)
. The extra space is the stack space. When reaching the last part of the linked-list the whole stack upto the previous node is stored.