Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode result = new ListNode(0);
ListNode p1 = l1;
ListNode p2 = l2;
if(p1.val<p2.val)
{
result = p1;
result.next = mergeTwoLists(p1.next,p2);
}
else
{
result = p2;
result.next = mergeTwoLists(p1,p2.next);
}
return result;
}
}