将两个已经排序的链表排成一个链表
其实就是归并排序
/**
* 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) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode tmp(-1);
// ListNode* head = (ListNode*)malloc(sizeof(ListNode));
ListNode *head = &tmp;
while( l1&&l2 )
{
if( (l1!=NULL)&&(l2!=NULL) )
{
if(l1->val < l2->val)
{ head->next = l1; l1=l1->next;}
else
{ head->next = l2; l2=l2->next;}
}
head = head->next;
}
if(l1)
head->next = l1;
if(l2)
head->next = l2;
return tmp.next;
}
};