Merge Two Sorted Lists

12/20/2017 update
When there is left over of l1 or l2, just append it to the result list. no need to use while loop.
res.next = l1.

-----------------------------------------------------
思路:
虽然是简单题,但是在linkedlist不熟的情况下自己做出来还是不错的。

两个linked list各取一个分别比大小,小的加入新的Linkedlist。 需要一个dummy node,来保存新的linkedlist的头,方便新的list往后扩展。

实现:
while循环判断条件是l1 != null && l2 != null, 然后出去了还得判断,不为空的是那一个list。 如果其中一个不为空那么另外一个list必为空,不然就不会退出while循环了。

评论

此博客中的热门博文

663. Equal Tree Partition

776. Split BST

426. Convert Binary Search Tree to Sored Doubly Linked List