{"id":6954,"date":"2025-06-18T14:52:34","date_gmt":"2025-06-18T09:22:34","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6954"},"modified":"2025-06-18T14:52:35","modified_gmt":"2025-06-18T09:22:35","slug":"merge-two-sorted-lists","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/merge-two-sorted-lists\/","title":{"rendered":"Merge Two Sorted Lists"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_main-heading {\n    text-align: center;\n    font-size: 2.4rem;\n    color: #E58C32;\n    margin-top: 2.5rem;\n    font-weight: bold;\n  }\n\n  .wp_blog_explanation,\n  .wp_blog_code-tabs-container {\n    max-width: 940px;\n    margin: 2rem auto;\n    padding: 2rem;\n    border-radius: 12px;\n    background-color: #f9fafb;\n  }\n\n  .wp_blog_explanation h2 {\n    font-size: 1.4rem;\n    color: #E58C32;\n    margin-bottom: 0.75rem;\n  }\n\n  .wp_blog_explanation p,\n  .wp_blog_explanation li {\n    font-size: 1.05rem;\n    line-height: 1.7;\n    margin: 0.5rem 0;\n    color: #1f2937;\n  }\n\n  .wp_blog_explanation code {\n    padding: 3px 6px;\n    border-radius: 4px;\n    background: #e5e7eb;\n    font-family: 'Courier New', monospace;\n  }\n\n  .wp_blog_code-tabs-header {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 0.5rem;\n    margin-bottom: 1rem;\n  }\n\n  .wp_blog_code-tab-button {\n    padding: 0.6rem 1.2rem;\n    border: 1px solid #E58C32;\n    color: #E58C32;\n    border-radius: 50px;\n    font-weight: 600;\n    cursor: pointer;\n    background-color: white;\n    transition: background 0.3s ease, color 0.3s ease;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: #E58C32;\n    color: white;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none;\n    background: #111827;\n    border-radius: 12px;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0;\n    padding: 1.5rem;\n    font-size: 1rem;\n    overflow-x: auto;\n    color: #f3f4f6;\n    background: #111827;\n    border-radius: 12px;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>You are given the heads of two sorted linked lists <code>list1<\/code> and <code>list2<\/code>.<\/p>\n  <p>Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.<\/p>\n  <p>Return the head of the merged linked list.<\/p>\n\n  <h2>Examples:<\/h2>\n  <p><strong>Input:<\/strong> list1 = [1,2,4], list2 = [1,3,4]<br><strong>Output:<\/strong> [1,1,2,3,4,4]<\/p>\n  <p><strong>Input:<\/strong> list1 = [], list2 = []<br><strong>Output:<\/strong> []<\/p>\n  <p><strong>Input:<\/strong> list1 = [], list2 = [0]<br><strong>Output:<\/strong> [0]<\/p>\n\n  <h2>Constraints:<\/h2>\n  <ul>\n    <li>The number of nodes in both lists is in the range [0, 50].<\/li>\n    <li>-100 &le; Node.val &le; 100<\/li>\n    <li>Both <code>list1<\/code> and <code>list2<\/code> are sorted in non-decreasing order.<\/li>\n  <\/ul>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Use a dummy node to simplify handling of the head.<\/li>\n    <li>Iterate through both lists and append the smaller value node to the merged list.<\/li>\n    <li>Once one list is exhausted, append the remaining part of the other list.<\/li>\n    <li>Return the merged list starting from <code>dummy.next<\/code>.<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"csharp\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nvar mergeTwoLists = function(list1, list2) {\n    if (!list1) return list2;\n    if (!list2) return list1;\n    let curr = null;\n    if (list1.val < list2.val) {\n        curr = list1;\n        list1 = list1.next;\n    } else {\n        curr = list2;\n        list2 = list2.next;\n    }\n    let start = curr;\n    while (list1 &#038;&#038; list2) {\n        if (list1.val < list2.val) {\n            curr.next = list1;\n            list1 = list1.next;\n        } else {\n            curr.next = list2;\n            list2 = list2.next;\n        }\n        curr = curr.next;\n    }\n    curr.next = list1 || list2;\n    return start;\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\nstruct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {\n    struct ListNode dummy = {0, NULL};\n    struct ListNode* curr = &dummy;\n\n    while (list1 && list2) {\n        if (list1->val < list2->val) {\n            curr->next = list1;\n            list1 = list1->next;\n        } else {\n            curr->next = list2;\n            list2 = list2->next;\n        }\n        curr = curr->next;\n    }\n    curr->next = list1 ? list1 : list2;\n    return dummy.next;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\nclass Solution {\npublic:\n    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n        ListNode dummy;\n        ListNode* curr = &dummy;\n\n        while (list1 && list2) {\n            if (list1->val < list2->val) {\n                curr->next = list1;\n                list1 = list1->next;\n            } else {\n                curr->next = list2;\n                list2 = list2->next;\n            }\n            curr = curr->next;\n        }\n        curr->next = list1 ? list1 : list2;\n        return dummy.next;\n    }\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\nclass Solution {\n    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n        ListNode dummy = new ListNode();\n        ListNode curr = dummy;\n\n        while (list1 != null && list2 != null) {\n            if (list1.val < list2.val) {\n                curr.next = list1;\n                list1 = list1.next;\n            } else {\n                curr.next = list2;\n                list2 = list2.next;\n            }\n            curr = curr.next;\n        }\n        curr.next = (list1 != null) ? list1 : list2;\n        return dummy.next;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Solution(object):\n    def mergeTwoLists(self, list1, list2):\n        dummy = ListNode(0)\n        curr = dummy\n\n        while list1 and list2:\n            if list1.val < list2.val:\n                curr.next = list1\n                list1 = list1.next\n            else:\n                curr.next = list2\n                list2 = list2.next\n            curr = curr.next\n\n        curr.next = list1 if list1 else list2\n        return dummy.next\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">\npublic class Solution {\n    public ListNode MergeTwoLists(ListNode list1, ListNode list2) {\n        ListNode dummy = new ListNode(0);\n        ListNode curr = dummy;\n\n        while (list1 != null && list2 != null) {\n            if (list1.val < list2.val) {\n                curr.next = list1;\n                list1 = list1.next;\n            } else {\n                curr.next = list2;\n                list2 = list2.next;\n            }\n            curr = curr.next;\n        }\n        curr.next = list1 ?? list2;\n        return dummy.next;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\n  document.addEventListener('DOMContentLoaded', () => {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n        buttons.forEach(btn => btn.classList.remove('active'));\n        contents.forEach(content => content.classList.remove('active'));\n        button.classList.add('active');\n        document.querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`).classList.add('active');\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Examples: Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":["post-6954","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6954"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6954\/revisions"}],"predecessor-version":[{"id":6955,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6954\/revisions\/6955"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}