题解 | #合并两个排序的链表#
合并两个排序的链表
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) {
if(list1==null&&list2==null)
return null;
if(list1==null)
return list2;
if(list2==null)
return list1;
ListNode p1 = list1;
ListNode p2 = list2;
ListNode p = new ListNode(0);
ListNode p3 = p;
while(p1!=null&&p2!=null){
ListNode temp;
if(p1.val<=p2.val){
temp = p1;
p1 = p1.next;
}else{
temp = p2;
p2 = p2.next;
}
p3.next = temp;
p3 = p3.next;
}
if(p1!=null){
p3.next = p1;
}
if(p2!=null){
p3.next = p2;
}
return p.next;
}
}