> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/linked-list/merge-two-sorted-lists.md).

# 12.Merge Two Sorted Lists

```
Example:
Input: 1->2->4  , 1->3->4
Output: 1->1->2->3->4->4
```

## Solution I: (Using extra space)

```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {

        ListNode *head = NULL;

        ListNode *p = l1;
        ListNode *q = l2;
        ListNode *r = NULL;
        while (p != NULL && q != NULL)
        {
            ListNode *temp = new ListNode;
            temp->val = p->val;
            temp->next = NULL;

            if (p->val <= q->val)
            {
                if (head == NULL)
                {
                    head = temp;
                    r = head;
                }
                else
                {
                    r->next = temp;
                    r = r->next;
                }
                p = p->next;
            }
            else
            {
                ListNode *temp = new ListNode;
                temp->val = q->val;
                temp->next = NULL;
                if (head == NULL)
                {
                    head = temp;
                    r = head;
                }
                else
                {
                    r->next = temp;
                    r = r->next;
                }
                q = q->next;
            }
        }

        while (p != NULL)
        {
            ListNode *temp = new ListNode;
            temp->val = p->val;
            temp->next = NULL;
            if (head == NULL)
            {
                head = temp;
                r = head;
            }
            else
            {
                r->next = temp;
                r = r->next;
            }
            p = p->next;
        }

        while (q != NULL)
        {
            ListNode *temp = new ListNode;
            temp->val = q->val;
            temp->next = NULL;
            if (head == NULL)
            {
                head = temp;
                r = head;
            }
            else
            {
                r->next = temp;
                r = r->next;
            }
            q = q->next;
        }

        return head;
    }
};
```

**Time Complexity O(m+n) and uses an Extra Space of O(n+m)**

## Solution II : (Using 3 pointers in constant space)

```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {

        ListNode *head = NULL;
        ListNode *p = l1;
        ListNode *q = l2;
        ListNode *sort = NULL;

        if (p == NULL)
        {
            return q;
        }
        if (q == NULL)
        {
            return p;
        }

        if (p->val <= q->val)
        {
            sort = p;
            p = sort->next;
        }
        else
        {
            sort = q;
            q = sort->next;
        }
        head = sort;

        while (p && q)
        {
            if (p->val <= q->val)
            {
                sort->next = p;
                sort = p;
                p = sort->next;
            }
            else
            {
                sort->next = q;
                sort = q;
                q = sort->next;
            }
        }

        if (p == NULL)
        {
            sort->next = q;
        }
        else if (q == NULL)
        {
             sort->next = p;
        }

        return head;
    }
};
```

**Time Complexity: O(m+n) and  Space Complexity: O(1)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soumyajit4419.gitbook.io/ds-algo/linked-list/merge-two-sorted-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
