주어진 두 개의 정렬된 연결리스트를 오름차순으로 병합하는 함수
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
var mergeTwoLists = function(list1, list2){
let currentNode = new ListNode();
let headNode = currentNode;
while(list1 !== null && list2 !== null){
if(list1.val < list2.val){
currentNode.next = list1;
list1 = list1.next;
} else {
currentNode.next = list2;
list2 = list2.next;
}
currentNode = currentNode.next;
}
currentNode.next = list1 !== null ? list1 : list2;
return headNode.next;
}
var mergeTwoLists = function(list1, list2){
if(list1 === null) return list2;
if(list2 === null) return list1;
if(list1.val < list2.val){
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}