如果一个链表为空,返回另一个即可
两个链表都不为空,判断两个链表头结点数据大小,将头结点指向较小的一条链表,接着递归找下一结点…
class ListNode{
ListNode next = null;
int data;
public ListNode(int data) {
this.data = data;
}
}
public class ListMerge{
public static ListNode ListMerge(ListNode l1,ListNode l2){
ListNode head = null;
if (l1 == null)return l2;
if (l2 == null)return l1;
if (l1.data<=l2.data){
head = l1;
head.next = ListMerge(l1.next,l2);
}else {
head = l2;
head.next = ListMerge(l1,l2.next);
}
return head;
}
}