public Node mergeTwoLists(Node l1, Node l2) {
if (l1==null ||l2==null) {
return l1!=null?l1:l2;
}
Node head = null;
if (l1.value<l2.value) {
head = l1;
l1 = l1.next;
} else {
head = l2;
l2 = l2.next;
}
Node last = head;
while (l1!=null&&l2!=null) {
if (l1.value<l2.value) {
last.next = l1;
l1 = l1.next;
} else {
last.next = l2;
l2 = l2.next;
}
last = last.next;
}
last.next = l1!=null?l1:l2;
return head;
}
网友评论