{"id":6950,"date":"2025-06-18T13:57:58","date_gmt":"2025-06-18T08:27:58","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6950"},"modified":"2025-06-18T13:58:00","modified_gmt":"2025-06-18T08:28:00","slug":"odd-even-linked-list","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/odd-even-linked-list\/","title":{"rendered":"Odd Even Linked List"},"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>Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.<\/p>\n  <p>The first node is considered odd (1st position), the second is even (2nd position), and so on.<\/p>\n\n  <h2>Example:<\/h2>\n  <p><strong>Input:<\/strong> head = [1,2,3,4,5]<br><strong>Output:<\/strong> [1,3,5,2,4]<\/p>\n  <p><strong>Input:<\/strong> head = [2,1,3,5,6,4,7]<br><strong>Output:<\/strong> [2,3,6,7,1,5,4]<\/p>\n\n  <h2>Constraints:<\/h2>\n  <ul>\n    <li>The number of nodes in the linked list is in the range [0, 10<sup>4<\/sup>].<\/li>\n    <li>-10<sup>6<\/sup> &le; Node.val &le; 10<sup>6<\/sup><\/li>\n  <\/ul>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Maintain two pointers: <code>odd<\/code> starting from the first node and <code>even<\/code> from the second.<\/li>\n    <li>Also store a reference to <code>evenHead<\/code> so that we can attach it to the end of the odd list later.<\/li>\n    <li>While traversing, link odd nodes together and even nodes together alternately.<\/li>\n    <li>Finally, link the last odd node to the head of the even list.<\/li>\n  <\/ul>\n\n  <h2>Time and Space Complexity:<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> O(n) \u2014 Each node is visited exactly once.<\/li>\n    <li><strong>Space Complexity:<\/strong> O(1) \u2014 In-place manipulation with constant extra space.<\/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  <!-- JavaScript -->\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nvar oddEvenList = function(head) {\n    if (!head || !head.next) return head;\n    let odd = head;\n    let even = head.next;\n    let evenHead = even;\n\n    while (even && even.next) {\n        odd.next = even.next;\n        odd = odd.next;\n        even.next = odd.next;\n        even = even.next;\n    }\n    odd.next = evenHead;\n    return head;\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <!-- C -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\nstruct ListNode* oddEvenList(struct ListNode* head) {\n    if (!head || !head->next) return head;\n    struct ListNode *odd = head, *even = head->next, *evenHead = even;\n\n    while (even && even->next) {\n        odd->next = even->next;\n        odd = odd->next;\n        even->next = odd->next;\n        even = even->next;\n    }\n    odd->next = evenHead;\n    return head;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <!-- C++ -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\nclass Solution {\npublic:\n    ListNode* oddEvenList(ListNode* head) {\n        if (!head || !head->next) return head;\n        ListNode* odd = head;\n        ListNode* even = head->next;\n        ListNode* evenHead = even;\n\n        while (even && even->next) {\n            odd->next = even->next;\n            odd = odd->next;\n            even->next = odd->next;\n            even = even->next;\n        }\n        odd->next = evenHead;\n        return head;\n    }\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <!-- Java -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\nclass Solution {\n    public ListNode oddEvenList(ListNode head) {\n        if (head == null || head.next == null) return head;\n        ListNode odd = head, even = head.next, evenHead = even;\n\n        while (even != null && even.next != null) {\n            odd.next = even.next;\n            odd = odd.next;\n            even.next = odd.next;\n            even = even.next;\n        }\n        odd.next = evenHead;\n        return head;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <!-- Python -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Solution(object):\n    def oddEvenList(self, head):\n        \"\"\"\n        :type head: Optional[ListNode]\n        :rtype: Optional[ListNode]\n        \"\"\"\n        if not head or not head.next:\n            return head\n        odd, even = head, head.next\n        even_head = even\n\n        while even and even.next:\n            odd.next = even.next\n            odd = odd.next\n            even.next = odd.next\n            even = even.next\n\n        odd.next = even_head\n        return head\n    <\/code><\/pre>\n  <\/div>\n\n  <!-- C# -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">\npublic class Solution {\n    public ListNode OddEvenList(ListNode head) {\n        if (head == null || head.next == null) return head;\n        ListNode odd = head, even = head.next, evenHead = even;\n\n        while (even != null && even.next != null) {\n            odd.next = even.next;\n            odd = odd.next;\n            even.next = odd.next;\n            even = even.next;\n        }\n\n        odd.next = evenHead;\n        return head;\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: Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd (1st position), the second is even (2nd position), and so on. Example: Input: head = [1,2,3,4,5]Output: [1,3,5,2,4] Input: head<\/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,176,245,810,174,172,173],"tags":[],"class_list":{"0":"post-6950","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-c-plus-plus","7":"category-csharp","8":"category-data-science-and-machine-learning","9":"category-dsa","10":"category-java","11":"category-javascript","12":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6950","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=6950"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6950\/revisions"}],"predecessor-version":[{"id":6971,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6950\/revisions\/6971"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}