short c++ solution,12ms


  • 0
    X
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    		struct ListNode *res = new ListNode(0);
    		ListNode *res_tmp = res;
    		while (l1 && l2) {
    			ListNode *&p = l1->val < l2->val ? l1 : l2;
    			res->next = p;
    			res = res->next;
    			p = p->next;
    		}
    
    	        res->next = l1 ? l1 : l2;
    	    
    		ListNode *head = res_tmp->next;
    		delete res_tmp;
    		return head;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.