LeetCode 21. Merge Two Sorted Lists
2022.07.18

問題

typescript

typescript
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
    if (list1 === null || list2 === null) {
        return list1 === null ? l2 : l1
    }
    let dummy = {}
    const init = dummy
    
    while (list1 !== null && list2 !== null) {
        if (list1.val > list2.val) {
            dummy.next = list2
            list2 = list2.next
        }
    }
};