Tianhe Gao

LC 21. Merge Two Sorted Listed

 1/**
 2 * Definition for singly-linked list.
 3 * function ListNode(val, next) {
 4 *     this.val = (val===undefined ? 0 : val)
 5 *     this.next = (next===undefined ? null : next)
 6 * }
 7 */
 8/**
 9 * @param {ListNode} list1
10 * @param {ListNode} list2
11 * @return {ListNode}
12 */
13var mergeTwoLists = function(list1, list2) {
14  if (list1 === null) {
15    return list2
16  } else if (list2 === null) {
17    return list1
18  } else if (list1.val < list2.val) {
19    list1.next = mergeTwoLists(list1.next, list2)
20    return list1
21  } else {
22    list2.next = mergeTwoLists(list1, list2.next)
23    return list2
24  }
25};

No notes link to this note

Welcome to tell me your thoughts via "email"
UP