</>

21. Merge Two Sorted Lists

Easy NeetCode 150
Linked List Time O(m+n) Space O(1)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Linked List
Time
O(m+n)
Space
O(1)

Standard Solution (C++)

SummaryGiven two sorted linked lists, produce a single sorted linked list by splicing together nodes from both inputs in non-decreasing order and returning the head of the merged result.

Approach
  • Use a dummy sentinel node to avoid special-casing the head; a cur pointer tracks the tail of the merged list being built.
  • At each step, compare the front values of both lists and attach the smaller node to cur->next, then advance that list's pointer and move cur forward.
  • Once one list is exhausted, append the remainder of the other directly — no further comparisons needed since both inputs are already sorted.
  • This in-place splice is O(1) extra space because we reuse the existing nodes rather than allocating new ones.
#include <cstddef>
using namespace std;

// 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* list1, ListNode* list2) {
        // Sentinel head eliminates edge-case handling for the first node.
        ListNode dummy;
        ListNode* tail = &dummy;

        // Repeatedly pick the smaller front node and attach it.
        while (list1 && list2) {
            if (list1->val <= list2->val) {
                tail->next = list1;
                list1 = list1->next;
            } else {
                tail->next = list2;
                list2 = list2->next;
            }
            tail = tail->next;
        }

        // Attach whichever list still has remaining nodes.
        tail->next = list1 ? list1 : list2;

        return dummy.next;
    }
};
Time O(m + n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

/**
* 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* list1, ListNode* list2) {
        ListNode dummy(0);
        ListNode* cur=&dummy;

        while(list1 && list2){
            if(list1->val<=list2->val){
                cur->next=list1;
                list1=list1->next;
            }
            else{
                cur->next=list2;
                list2=list2->next;
            }
            cur=cur->next;
        }

        if(list1)
            cur->next=list1;
        else if(list2)
            cur->next=list2;

        return dummy.next;
    }
};

// Time Complexity: O(m+n)
// Space Complexity: O(1)
Time O(m+n) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern