Straight forward C++


  • 0
    C
    class Solution {
    public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* result_flag=new ListNode(1);
        ListNode* return_flag=result_flag;
        ListNode* first_flag=l1;
        ListNode* second_flag=l2;
        while(first_flag||second_flag){
            if(!first_flag){
                result_flag->next=second_flag;
                return return_flag->next;
            }
            if(!second_flag){
                result_flag->next=first_flag;
                return return_flag->next;
            }
            if(first_flag->val>=second_flag->val){
                result_flag->next=second_flag;
                second_flag=second_flag->next;
                result_flag=result_flag->next;
            }
            else{
                result_flag->next=first_flag;
                first_flag=first_flag->next;
                result_flag=result_flag->next;
            }
        }
        return return_flag->next;
        
    }
    };

Log in to reply
 

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