http://oj.leetcode.com/problems/merge-two-sorted-lists/
/**
* 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;
else if(l2==NULL) return l1;
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(l1->val>l2->val) {
ListNode* tmpNode = l1;
l1 = l2;
l2 = tmpNode;
}
ListNode* head=l1;
ListNode* current=l1;
l1=l1->next;
while(l1!=NULL&&l2!=NULL){
if(l1->val<l2->val){
current->next=l1;
current=current->next;
l1=l1->next;
}
else{
current->next=l2;
current=current->next;
l2=l2->next;
}
}
if(l1==NULL) current->next=l2;
else current->next=l1;
return head;
}
};