/* 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 result = new ListNode(0); ListNode p1=list1,p2=list2,p3=result; while(p1!=null||p2!=null){ p3.next=new ListNode(0); p3=p3.next; if(p1==null&&p2!=null){ p3.val=p2.val; p2 = p2.next; } if(p2==null&&p1!=null){ p3.val=p1.val; p1=p1.next; } if(p1!=null&&p2!=null){ if(p1.val>p2.val){ p3.val=p2.val; p2=p2.next; }else{ p3.val=p1.val; p1=p1.next; } } p3.next=null; } return result.next; } }