Reverse Linked List
Last updated
Last updated
Given the head
of a singly linked list, reverse the list, and return the reversed list.
The time complexity of the algorithm is O(n), where n is the number of nodes in the linked list. This is because each node in the list is visited exactly once during the reversal process.
The space complexity of the algorithm is O(1). This is because the amount of extra space used does not grow with the size of the input linked list, and is therefore constant. The algorithm only uses a fixed amount of space to store pointers (i.e., current
, prev
, and nextTemp
), regardless of the list size.