LeetCode 21. Merge Two Sorted Lists

題目本身沒什麼難度,但是這個題用 C++ 的話需要用它給你的那個鏈表結構,十分麻煩,而且不方便添加哨兵。。
JavaScript:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var mergeTwoLists = function(l1, l2) {
    var head = new ListNode(0), ptr = head;
    
    while (l1 !== null || l2 !== null){
        if (l1 !== null && (l2 === null || l1.val < l2.val)){
            ptr.next = new ListNode(l1.val);
            l1 = l1.next;
        }
        else{
            ptr.next = new ListNode(l2.val);
            l2 = l2.next;
        }
        ptr = ptr.next;
    }
    
    return head.next;
};