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.
分析:
就是使用两个指针,让较小的归并到新的list上:
代码:
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
方法二:递归
该方法并不是尾递归(tail-recursive),所以当list过长,stack会溢出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|