将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
题解:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function Node(value){
this.value = value;
this.next = null
}
var a =new Node(parseInt(window.prompt('input')));
var b =new Node(parseInt(window.prompt('input')));
var c =new Node(parseInt(window.prompt('input')));
var d =new Node(parseInt(window.prompt('input')));
var e =new Node(parseInt(window.prompt('input')));
var f =new Node(parseInt(window.prompt('input')));
a.next = b;
b.next = c;
d.next = e;
e.next = f;
function compare(l1,l2){
if(l1 === null){
var newNode = l2.next
if(newNode){
if(l2.value > newNode)
}
return l2;
}else if(l2 === null ){
return l1
}else if(l1.value < l2.value){
l1.next = compare(l1.next,l2)
return l1
}else if(l1.value > l2.value){
l2.next = compare(l2.next,l1)
return l2
}else{
l1.next = compare(l1.next,l2)
return l1
}
}
console.log(compare(a,d))
</script>
</body>
</html>