{"id":651,"date":"2025-07-15T13:11:00","date_gmt":"2025-07-15T07:41:00","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=651"},"modified":"2025-07-15T13:11:01","modified_gmt":"2025-07-15T07:41:01","slug":"odd-even-linked-list-leetcode-328","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/","title":{"rendered":"Odd Even Linked List | Leetcode 328 | Brute to Optimal Solution"},"content":{"rendered":"\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return&nbsp;<em>the reordered list<\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>first<\/strong>&nbsp;node is considered&nbsp;<strong>odd<\/strong>, and the&nbsp;<strong>second<\/strong>&nbsp;node is&nbsp;<strong>even<\/strong>, and so on.<\/p>\n\n\n\n<p>Note that the relative order inside both the even and odd groups should remain as it was in the input.<\/p>\n\n\n\n<p>You must solve the problem&nbsp;in&nbsp;<code>O(1)<\/code>&nbsp;extra space complexity and&nbsp;<code>O(n)<\/code>&nbsp;time complexity.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/10\/oddeven-linked-list.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1,2,3,4,5]<br><strong>Output:<\/strong> [1,3,5,2,4]<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/10\/oddeven2-linked-list.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [2,1,3,5,6,4,7]<br><strong>Output:<\/strong> [2,3,6,7,1,5,4]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number of nodes in the linked list is in the range&nbsp;<code>[0, 10<sup>4<\/sup>]<\/code>.<\/li>\n\n\n\n<li><code>-10<sup>6<\/sup> &lt;= Node.val &lt;= 10<sup>6<\/sup><\/code><\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-d0526e5c-0650-4fe7-9a84-56ea2bd87a2d\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#0-solution-1-brute-force-approach-using-array\" style=\"\">Solution 1: Brute Force Approach (Using Array)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#8-solution-2-optimal-approach-pointer-manipulation\" style=\"\">Solution 2: Optimal Approach (Pointer Manipulation)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#9-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#10-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#11-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#12-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#13-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#14-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#15-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/odd-even-linked-list-leetcode-328\/#16-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-1-brute-force-approach-using-array\">Solution 1: Brute Force Approach (Using Array)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like organizing a line of people! You want all the people in odd positions (1st, 3rd, 5th&#8230;) to go first, then all the people in even positions (2nd, 4th, 6th&#8230;) to follow. One simple way is to write down all the odd-position people&#8217;s names first, then all the even-position people&#8217;s names, and finally rearrange everyone according to your list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>First Pass<\/strong>: Walk through the linked list and collect all values from odd-positioned nodes (1st, 3rd, 5th&#8230;)<\/li>\n\n\n\n<li><strong>Second Pass<\/strong>: Walk through the linked list again and collect all values from even-positioned nodes (2nd, 4th, 6th&#8230;)<\/li>\n\n\n\n<li><strong>Combine Arrays<\/strong>: Now we have all odd-position values followed by all even-position values<\/li>\n\n\n\n<li><strong>Third Pass<\/strong>: Walk through the original linked list and replace each node&#8217;s value with values from our combined array<\/li>\n\n\n\n<li><strong>Return Result<\/strong>: The linked list now has the desired odd-even arrangement<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def oddEvenList(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        if head is None or head.next is None:\n            return head  # Empty list or single node - nothing to rearrange\n\n        temp = head\n        values = []\n\n        # First pass: Collecting values from odd-indexed nodes (1st, 3rd, 5th, ...)\n        while temp:\n            values.append(temp.val)  # Add current node's value\n            if temp.next:\n                temp = temp.next.next  # Skip one node to get next odd position\n            else:\n                break  # Reached end of list\n\n        temp = head.next  # Start from 2nd node (first even position)\n\n        # Second pass: Collecting values from even-indexed nodes (2nd, 4th, 6th, ...)\n        while temp:\n            values.append(temp.val)  # Add current node's value\n            if temp.next:\n                temp = temp.next.next  # Skip one node to get next even position\n            else:\n                break  # Reached end of list\n\n        # Third pass: Assigning collected values back to the nodes\n        index = 0\n        temp = head\n        while temp:\n            temp.val = values[index]  # Replace node value with rearranged value\n            index += 1\n            temp = temp.next\n\n        return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">oddEvenList<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode]) -&gt; Optional[ListNode]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> head <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> head.next <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head  <\/span><span style=\"color: #6A9955\"># Empty list or single node - nothing to rearrange<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        values = []<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># First pass: Collecting values from odd-indexed nodes (1st, 3rd, 5th, ...)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            values.append(temp.val)  <\/span><span style=\"color: #6A9955\"># Add current node&#39;s value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                temp = temp.next.next  <\/span><span style=\"color: #6A9955\"># Skip one node to get next odd position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Reached end of list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head.next  <\/span><span style=\"color: #6A9955\"># Start from 2nd node (first even position)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Second pass: Collecting values from even-indexed nodes (2nd, 4th, 6th, ...)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            values.append(temp.val)  <\/span><span style=\"color: #6A9955\"># Add current node&#39;s value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                temp = temp.next.next  <\/span><span style=\"color: #6A9955\"># Skip one node to get next even position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Reached end of list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Third pass: Assigning collected values back to the nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        index = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp.val = values[index]  <\/span><span style=\"color: #6A9955\"># Replace node value with rearranged value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            index += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use three separate passes through the linked list. In the first pass, we start from the head (1st node) and jump two nodes at a time to collect all odd-positioned values. In the second pass, we start from the second node and again jump two nodes at a time to collect all even-positioned values. Now our array has all odd values followed by all even values in the correct order. In the third pass, we simply replace each node&#8217;s value with the values from our rearranged array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/code><\/p>\n\n\n\n<p><strong>First Pass (Odd positions):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at node 1: values =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, jump to node 3<\/li>\n\n\n\n<li>At node 3: values =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, jump to node 5<\/li>\n\n\n\n<li>At node 5: values =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, no more nodes<\/li>\n<\/ul>\n\n\n\n<p><strong>Second Pass (Even positions):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at node 2: values =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, jump to node 4<\/li>\n\n\n\n<li>At node 4: values =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, no more nodes<\/li>\n<\/ul>\n\n\n\n<p><strong>Third Pass (Replace values):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Node 1: value = 1 (values)<\/li>\n\n\n\n<li>Node 2: value = 3 (values<a href=\"https:\/\/leetcode.com\/problems\/odd-even-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>)<\/li>\n\n\n\n<li>Node 3: value = 5 (values)<\/li>\n\n\n\n<li>Node 4: value = 2 (values)<\/li>\n\n\n\n<li>Node 5: value = 4 (values)<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 3 -&gt; 5 -&gt; 2 -&gt; 4<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(n) &#8211; We traverse the list three times, but it&#8217;s still linear<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(n) &#8211; We store all node values in the array<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like making a guest list for a party &#8211; first write down all the odd-numbered guests, then all the even-numbered guests, and finally rearrange everyone according to your list. It&#8217;s easy to understand but requires extra space to store all the values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-solution-2-optimal-approach-pointer-manipulation\">Solution 2: Optimal Approach (Pointer Manipulation)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition\">Intuition<\/h3>\n\n\n\n<p>Instead of writing down names and rearranging, what if we could just change who&#8217;s standing next to whom? Imagine you have two group leaders &#8211; one for odd positions and one for even positions. Each leader collects their group members by changing the hand-holding pattern. At the end, the odd group leader connects their group to the even group!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set Up Group Leaders<\/strong>: Create two pointers &#8211; one for odd positions starting at head, one for even positions starting at head.next<\/li>\n\n\n\n<li><strong>Remember Even Start<\/strong>: Keep track of where the even group starts so we can connect it later<\/li>\n\n\n\n<li><strong>Collect Odd Group<\/strong>: Make odd pointer jump two nodes at a time, connecting only odd-positioned nodes<\/li>\n\n\n\n<li><strong>Collect Even Group<\/strong>: Make even pointer jump two nodes at a time, connecting only even-positioned nodes<\/li>\n\n\n\n<li><strong>Connect Groups<\/strong>: Connect the end of the odd group to the beginning of the even group<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def oddEvenList(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        if not head or not head.next:\n            return head  # Empty list or single node - nothing to rearrange\n\n        odd = head          # Pointer for odd-positioned nodes\n        even = head.next    # Pointer for even-positioned nodes\n        even_head = even    # Remember where even group starts\n\n        # Traverse the list, rearranging odd and even nodes\n        while even and even.next:\n            odd.next = odd.next.next    # Connect current odd to next odd\n            odd = odd.next              # Move odd pointer forward\n            even.next = even.next.next  # Connect current even to next even\n            even = even.next            # Move even pointer forward\n\n        # Append the even list to the end of the odd list\n        odd.next = even_head\n\n        return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">oddEvenList<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode]) -&gt; Optional[ListNode]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> head <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> head.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head  <\/span><span style=\"color: #6A9955\"># Empty list or single node - nothing to rearrange<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        odd = head          <\/span><span style=\"color: #6A9955\"># Pointer for odd-positioned nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        even = head.next    <\/span><span style=\"color: #6A9955\"># Pointer for even-positioned nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        even_head = even    <\/span><span style=\"color: #6A9955\"># Remember where even group starts<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Traverse the list, rearranging odd and even nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> even <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> even.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            odd.next = odd.next.next    <\/span><span style=\"color: #6A9955\"># Connect current odd to next odd<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            odd = odd.next              <\/span><span style=\"color: #6A9955\"># Move odd pointer forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            even.next = even.next.next  <\/span><span style=\"color: #6A9955\"># Connect current even to next even<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            even = even.next            <\/span><span style=\"color: #6A9955\"># Move even pointer forward<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Append the even list to the end of the odd list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        odd.next = even_head<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use two pointers to build two separate chains &#8211; one for odd positions and one for even positions. The odd pointer starts at the head and connects every odd-positioned node by skipping the even ones. Similarly, the even pointer starts at the second node and connects every even-positioned node by skipping the odd ones. We continue this until we reach the end of the list. Finally, we connect the end of the odd chain to the beginning of the even chain, creating the desired arrangement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/code><\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>odd = 1, even = 2, even_head = 2<\/li>\n\n\n\n<li>Current state:&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>odd.next = 3 (skip node 2):&nbsp;<code>1 -&gt; 3 -&gt; 4 -&gt; 5<\/code>, odd = 3<\/li>\n\n\n\n<li>even.next = 4 (skip node 3):&nbsp;<code>2 -&gt; 4 -&gt; 5<\/code>, even = 4<\/li>\n\n\n\n<li>Current odd chain:&nbsp;<code>1 -&gt; 3<\/code>, Current even chain:&nbsp;<code>2 -&gt; 4<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>odd.next = 5 (skip node 4): odd chain becomes&nbsp;<code>1 -&gt; 3 -&gt; 5<\/code>, odd = 5<\/li>\n\n\n\n<li>even.next = None (no more nodes): even chain becomes&nbsp;<code>2 -&gt; 4<\/code>, even = None<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Connection:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>odd.next = even_head: Connect&nbsp;<code>1 -&gt; 3 -&gt; 5<\/code>&nbsp;to&nbsp;<code>2 -&gt; 4<\/code><\/li>\n\n\n\n<li>Final result:&nbsp;<code>1 -&gt; 3 -&gt; 5 -&gt; 2 -&gt; 4<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 3 -&gt; 5 -&gt; 2 -&gt; 4<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(n) &#8211; We traverse the list once<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1) &#8211; We only use a few pointer variables<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like having two team captains who pick their team members alternately, but instead of moving people around, they just change who&#8217;s holding whose hand. Much more efficient since we don&#8217;t need extra space &#8211; we just rewire the connections!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"16-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Brute Force (Array)<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Easy<\/td><td>Learning the concept<\/td><\/tr><tr><td>Optimal (Pointer Manipulation)<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>optimal approach<\/strong>&nbsp;is generally preferred because it uses constant space while maintaining the same time efficiency. However, the brute force approach is more intuitive and easier to understand when you&#8217;re first learning about linked list manipulation.<\/p>\n\n\n\n<p>Both solutions effectively solve the problem, but the optimal solution showcases advanced pointer manipulation techniques that are valuable for solving many other linked list problems. The key insight is that we don&#8217;t need to store values separately, we can just rewire the connections between nodes!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;head&nbsp;of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return&nbsp;the reordered list. The&nbsp;first&nbsp;node is considered&nbsp;odd, and the&nbsp;second&nbsp;node is&nbsp;even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You<\/p>\n","protected":false},"author":1,"featured_media":652,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,29],"class_list":{"0":"post-651","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-singly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/odd-even-linked-list-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/651\/revisions\/654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/652"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}