题目
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
code
var mergeTwoLists = function(l1, l2) {
var p1 = l1
var p2 = l2
var head = new ListNode(null)
var cur = head
while (p1 && p2) {
if (p1.val > p2.val) {
cur.next = p2
p2 = p2.next
} else {
cur.next = p1
p1 = p1.next
}
cur = cur.next
}
if (p1) cur.next = p1
if (p2) cur.next = p2
return head.next
};
网友评论