public class ListNode {
int val;
ListNode next = null;
public ListNode(int val) {
this.val = val;
}
}
public class Test {
/**
*
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
public ListNode Merge (ListNode pHead1, ListNode pHead2) {
// write code here
if(pHead1==null){
return pHead2;
}
if(pHead2==null){
return pHead1;
}
ListNode curr=new ListNode(-1);
ListNode head=curr;
while(pHead1!=null && pHead2!=null){
if(pHead1.val<=pHead2.val){
curr.next=pHead1;
pHead1=pHead1.next;
}else{
curr.next=pHead2;
pHead2=pHead2.next;
}
curr=curr.next;
}
curr.next= pHead1==null? pHead2:pHead1;
return head.next;
}
}