문제 링크 : Merge Two Sorted Lists
/**
* 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 List = new ListNode(0);
let head = List;
let sortedArr = []
while(list1 !== null || list2 !== null){
if(list1 !== null) {
sortedArr.push(list1.val)
list1 = list1.next
}
if(list2 !== null ) {
sortedArr.push(list2.val)
list2 = list2.next
}
}
sortedArr.sort( (a,b) => (a-b) )
for(let i=0; i<sortedArr.length; i++){
head.next = new ListNode(sortedArr[i])
head = head.next
}
return List.next
};
/**
* 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(a, b) {
var c = root = {val:0, next:null}
while (true) {
if (a===null) {c.next=b; return root.next}
if (b===null) {c.next=a; return root.next}
if (a.val < b.val) {c=c.next=a; a=a.next}
else {c=c.next=b; b=b.next}
}
};