/*
* @lc app=leetcode id=21 lang=cpp
*
* [21] Merge Two Sorted Lists
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL || l2 == NULL){
if(l1 == NULL) return l2;
return l1;
}
ListNode *head = new ListNode();
ListNode *nowA = head;
ListNode *nowB = l1;
ListNode *now2 = l2;
ListNode *tmp = NULL;
nowA->next = nowB;
while(nowB !=NULL && now2 != NULL){
if(now2->val < nowB->val){
tmp = now2->next;
nowA->next = now2;
now2->next = nowB;
now2 = tmp;
nowA = nowA->next;
}else{
nowA = nowB;
nowB = nowB->next;
}
}
if(now2 != NULL) nowA->next = now2;
return head->next;
}
};
// @lc code=end