Question
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.
My Solution
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* rst;
if(NULL == l1)
{
rst = l2;
return rst;
}
if(NULL == l2)
{
rst = l1;
return l1;
}
if(l1->val > l2->val)
{
rst = l2;
l2 = l2->next;
}else
{
rst = l1;
l1 = l1->next;
}
rst->next = mergeTwoLists(l1, l2);
return rst;
}
};