# Leetcode No.21 **[題目連結](https://leetcode.com/problems/merge-two-sorted-lists/description/)** ## JavaScript ```javascript= /** * 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) { if (!list1){ return list2; } if (!list2){ return list1; } var current_node =new ListNode(0,null); var result = current_node; while (list1 != null && list2 != null){ if (list1.val <= list2.val){ current_node.next = new ListNode(list1.val,null); current_node = current_node.next; list1 = list1.next; } else{ current_node.next = new ListNode(list2.val,null); current_node = current_node.next; list2 = list2.next; } } if (list1 == null){ current_node.next = list2; } else{ current_node.next = list1; } result = result.next; return result; } ```
{}