21. Merge Two Sorted Lists
problem description
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
algorithm thought
之前也提到了,碰到链表题并且返回值是链表的情况下,可以首先考虑能否用递归解题。这题正好可以。和之前链表加法很相似,可能还简单一点
code
algorithm analysis
时间复杂度O(n)的,递归肯定是比迭代方法慢一点的,但是两种方法时间复杂度是一样的。之后有时间来实现以下迭代版本的把。
Last updated