08. Merge 2 sorted linked lists in reverse order
The problem can be found at the following link: Question Link
My Approach
Defined a
reverseList
to reverse a linked list recursively.Created a
mergeResult
to merge two sorted linked lists in reverse order.Create a new node
curr
and ahead
node to keep track of the merged list.Traverse both lists, comparing elements and merging them in descending order.
Update
curr
as you traverse.After one list is exhausted, append the remaining elements of the other list to
curr
.Return the reversed merged list.
Time and Auxiliary Space Complexity
Time Complexity:
O(N + M)
where N and M are the lengths of the two linked lists.Auxiliary Space Complexity: O(1)
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated