[LeetCode][21. Merge Two Sorted Lists] The Recursive Algorithm with Detailed Image Explanation
By Long Luo
This article is the solution The Recursive Algorithm with Detailed Image Explanation of Problem 21. Merge Two Sorted Lists.
Intuition
We can use Recursion to solve this problem.
The key points of Recursion are .
-
How to terminate the recursion: Returns when either or is .
-
What to do in the process: if , then point to the smaller of and .
If , we can choose the smaller node, such as . However, the linked list is not reached the end, we will continue to compare.
Now we are compare and . The and are processed in the recursive functions of the next layer.
We process such process and finally get the result.
Image Explanation
You can get a intuition from below images.
1 | class Solution { |
Analysis
- Time Complexity: .
- Space Complexity: .
All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)
Explore More Leetcode Solutions. 😉😃💗