题解 | #合并两个排序的链表#
合并两个排序的链表
http://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode we =list1;
ListNode you= list1;
if(list1 == null && list2 == null){
return null;
}
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
if(list1.val <= list2.val){
we=list1;
list1=list1.next;
}
else{
we = list2;
list2 = list2.next;
}
you = we;
int listone=0;
while(true){
if(list1 == null){
listone = 1;
break;
}
if(list2 == null){
break;
}
if(list1.val <= list2.val){
we.next = list1;
list1 = list1.next;
we = we.next;
}
else{
we.next = list2;
list2 = list2.next;
we = we.next;
}
}
if(listone == 1){
we.next = list2;
return you;
}
else{
we.next = list1;
return you;
}
}
}