首页 > 代码库 > 两个链表合并不加入新的链表空间

两个链表合并不加入新的链表空间

class LNode {
	int value;
	LNode next;

	public LNode(int value, LNode next) {
		this.value = http://www.mamicode.com/value;",");
			} else {
				System.out.print(merge.value);
			}

			merge = merge.next;
		}

	}

	public static LNode mergeLinks(LNode head1,LNode head2){
		if(head1==null){
			return head2;
		}
		if(head2==null){
			return head1;
		}
		LNode head;
		if(head1.value>head2.value){
			head = head2;
			head2=head2.next;
		}else{
			head = head1;
			head1=head1.next;
		}
		LNode current=head;//current指向新的链表的最后一个节点
		while(head1!=null && head2!=null){
			if(head1.value>head2.value){
				current.next=head2;
				current=head2;
				head2=head2.next;
			}else{
				current.next=head1;
				current = head1;
				head1=head1.next;
			}
		}
		if(head1!=null){//把listA的全部插入到current后面
			current.next=head1;
		}
		if(head2!=null){
			current.next=head2;
		}
		
		return head;
		
	}

}

 

两个链表合并不加入新的链表空间