# Des:

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.

eg：

image.png

# Question:

``````/**
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {

};
``````

# Mind:

mergeTwoLists 函数主要来实现合并2个链表。

``````var mergeTwoLists = function(l1, l2) {
if(l1 ===null) return l2;
if(l2 ===null) return l1;

var t; //定义变量 引用l1/l2

if(l1.val<l2.val){
t=l1;
t.next=mergeTwoLists(l1.next,l2); //注意递归传入的成员是l1.next的，l1自身的已被保存在t上
}else{
t=l2; //在函数在递归时转到else 分支时，虽然t=l2，影响了t的值，但是只是在递归中印象，它最外层的t是已经确定了的（因为代码顺序执行时，最外层的t已经定了）
t.next=mergeTwoLists(l1,l2.next);
}

return t;
};
``````

# Source URL:

<a href="https://leetcode.com/problems/merge-two-sorted-lists/#/description">leetcode传送门</a>
<a href="https://github.com/yuanhaoyu/leetcode-review/blob/master/2017/21.js">codeDemo传送门</a>