You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:

Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = [] Output: []
Example 3:
Input: list1 = [], list2 = [0] Output: [0]
Constraints:
- The number of nodes in both lists is in the range
[0, 50]. -100 <= Node.val <= 100- Both
list1andlist2are sorted in non-decreasing order.
對我來說,解 linked list 系列的圖目,就是畫圖, 畫圖, 畫圖。

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function(list1, list2) {
let newNode = new ListNode()
let start = newNode
while (list1 && list2) {
if (list1.val <= list2.val) {
newNode.next = new ListNode(list1.val, null)
list1 = list1.next
} else {
newNode.next = new ListNode(list2.val, null)
list2 = list2.next
}
newNode = newNode.next
}
newNode.next = list1 ? list1 : list2
return start.next
};
首先,我們要設置一個新的 node 當做開頭。

開始逐一比較兩個 list 的數字,把小的那一個的值做成新的 node。

一直重複,直到其中一個 list 的 node 全部用完。

把最後一個節點直接連到沒用完的 list 讓它繼續排下去。

由於起點 start 並不是答案要的開頭,他的下一個 node 才是兩個 list 組合後的新 head,所以 start 的下一個 node 才是我們要回傳的物件。
