递归实现
class Node{
Node next=null;
int data;
public Node(int data) {
this.data=data;
}
}
public class MergeList {
/**
* 使用递归
*/
public static Node mergeList(Node head1,Node head2) {
if(head1==null)
return head2;
if (head2==null) {
return head1;
}
Node head=null;
if(head1.data<head2.data) {
head=head1;
head.next=mergeList(head1.next, head2);
}else {
head=head2;
head.next=mergeList(head1, head2.next);
}
return head;
}
}