{"id":628,"date":"2025-07-15T12:04:58","date_gmt":"2025-07-15T06:34:58","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=628"},"modified":"2025-07-15T12:05:13","modified_gmt":"2025-07-15T06:35:13","slug":"middle-of-the-linked-list-leetcode-876","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/","title":{"rendered":"Middle of the Linked List | Leetcode 876 | Tortoise Hare Approach"},"content":{"rendered":"\n<p>The &#8220;Middle of the Linked List&#8221; problem is a classic linked list question that helps you understand traversal techniques and the famous two-pointer approach. <\/p>\n\n\n\n<p>In this blog, we&#8217;ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with detailed explanations, code comments, dry runs, and clear reasoning.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/middle-of-the-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<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-30b6ea19-d28e-4b35-a8d1-c206c9344ffd\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" 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\/middle-of-the-linked-list-leetcode-876\/#0-brute-force-solution-count-then-traverse\" style=\"\">Brute Force Solution (Count then Traverse)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#4-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#7-optimal-solution-two-pointers-fast-and-slow\" style=\"\">Optimal Solution (Two Pointers \/ Fast and Slow)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#11-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/middle-of-the-linked-list-leetcode-876\/#14-final-thoughts\" style=\"\">Final Thoughts<\/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<p>Given the&nbsp;<code>head<\/code>&nbsp;of a singly linked list, return&nbsp;<em>the middle node of the linked list<\/em>.<\/p>\n\n\n\n<p>If there are two middle nodes, return&nbsp;<strong>the second middle<\/strong>&nbsp;node.<\/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\/07\/23\/lc-midlist1.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> [3,4,5]<br><strong>Explanation:<\/strong> The middle node of the list is node 3.<\/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\/07\/23\/lc-midlist2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1,2,3,4,5,6]<br><strong>Output:<\/strong> [4,5,6]<br><strong>Explanation:<\/strong> Since the list has two middle nodes with values 3 and 4, we return the second one.<\/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 list is in the range\u00a0<code>[1, 100]<\/code>.<\/li>\n\n\n\n<li><code>1 &lt;= Node.val &lt;= 100<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution-count-then-traverse\">Brute Force Solution (Count then Traverse)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let&#8217;s think about this problem step by step in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Why do we need to count first?<\/strong><br>To find the middle of a linked list, we need to know how many nodes there are. Unlike arrays where we can directly access elements by index, linked lists require us to traverse from the beginning to reach any specific position.<\/li>\n\n\n\n<li><strong>How do we find the middle position?<\/strong><br>Once we know the total count\u00a0<code>n<\/code>, the middle position is at index\u00a0<code>n \/\/ 2<\/code>\u00a0(0-indexed). This works for both odd and even lengths:\n<ul class=\"wp-block-list\">\n<li>For odd length (e.g., 5 nodes): middle is at index 5\/\/2 = 2 (3rd node)<\/li>\n\n\n\n<li>For even length (e.g., 6 nodes): middle is at index 6\/\/2 = 3 (4th node, which is the second middle)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Two-pass approach:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>First pass:<\/strong>\u00a0Count all nodes by traversing the entire list<\/li>\n\n\n\n<li><strong>Second pass:<\/strong>\u00a0Start from head again and move exactly\u00a0<code>n \/\/ 2<\/code>\u00a0steps to reach the middle<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>Since we know the exact position of the middle node, we can directly navigate to it. This approach is reliable and easy to understand.<\/li>\n\n\n\n<li><strong>When should you use this approach?<\/strong><br>This method is great when you want to be absolutely clear about what you&#8217;re doing, or when you need to know the total length for other purposes too.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/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 middleNode(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        n = 0\n        temp = head\n        # First pass: count the total number of nodes\n        while temp:\n            n += 1                          # increment counter\n            temp = temp.next                # move to next node\n        \n        temp = head\n        # Second pass: move to the middle position\n        for i in range(n \/\/ 2):\n            temp = temp.next                # move forward n\/\/2 steps\n        return temp                         # return the middle node\" 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\">middleNode<\/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\">        n = <\/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: #6A9955\"># First pass: count the total number of nodes<\/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\">            n += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">                          <\/span><span style=\"color: #6A9955\"># increment counter<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next                <\/span><span style=\"color: #6A9955\"># move to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/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: #6A9955\"># Second pass: move to the middle position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next                <\/span><span style=\"color: #6A9955\"># move forward n\/\/2 steps<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> temp                         <\/span><span style=\"color: #6A9955\"># return the middle node<\/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=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p><strong>Step 1: Count the nodes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use a temporary pointer\u00a0<code>temp<\/code>\u00a0to traverse the list without losing the original head<\/li>\n\n\n\n<li>We start counting from 0 and increment for each node we visit<\/li>\n\n\n\n<li>When\u00a0<code>temp<\/code>\u00a0becomes\u00a0<code>None<\/code>, we&#8217;ve counted all nodes<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: Find the middle<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We reset\u00a0<code>temp<\/code>\u00a0to point back to the head<\/li>\n\n\n\n<li>We move forward exactly\u00a0<code>n \/\/ 2<\/code>\u00a0steps using a for loop<\/li>\n\n\n\n<li>After the loop,\u00a0<code>temp<\/code>\u00a0points to the middle node<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: Return the result<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We return\u00a0<code>temp<\/code>, which now points to the middle node<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through this with an example:<\/p>\n\n\n\n<p><strong>Input:<\/strong><br><code>head = [1]<\/code><\/p>\n\n\n\n<p><strong>First Pass (Counting):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp = 1, n = 1<\/li>\n\n\n\n<li>temp = 2, n = 2<\/li>\n\n\n\n<li>temp = 3, n = 3<\/li>\n\n\n\n<li>temp = 4, n = 4<\/li>\n\n\n\n<li>temp = 5, n = 5<\/li>\n\n\n\n<li>temp = None, exit loop, total n = 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Second Pass (Finding Middle):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n \/\/ 2 = 5 \/\/ 2 = 2, so we need to move 2 steps<\/li>\n\n\n\n<li>temp = 1 (start)<\/li>\n\n\n\n<li>i = 0: temp = 2 (move 1 step)<\/li>\n\n\n\n<li>i = 1: temp = 3 (move 2 steps)<\/li>\n\n\n\n<li>Return node with value 3<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br>Node with value 3 (and the rest of the list: )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n) + O(n\/2) = O(n)<br>First pass takes O(n) to count, second pass takes O(n\/2) to reach middle<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only using a few variables regardless of input size<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is straightforward and easy to understand. It&#8217;s perfect for beginners to grasp the concept, though it requires two passes through the list.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-optimal-solution-two-pointers-fast-and-slow\">Optimal Solution (Two Pointers \/ Fast and Slow)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Now let&#8217;s think about a more elegant solution using the&nbsp;<strong>two-pointer technique<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>The core idea: Different speeds<\/strong><br>Imagine two people running on a track. If one person runs twice as fast as the other, when the faster person completes the track, the slower person will be exactly at the middle. This is the exact principle we use here.<\/li>\n\n\n\n<li><strong>Why does the two-pointer approach work?<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Slow pointer:<\/strong>\u00a0Moves 1 step at a time<\/li>\n\n\n\n<li><strong>Fast pointer:<\/strong>\u00a0Moves 2 steps at a time<\/li>\n\n\n\n<li>When the fast pointer reaches the end, the slow pointer will be at the middle<\/li>\n\n\n\n<li>This is because the fast pointer covers twice the distance in the same time<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Mathematical reasoning:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the list has\u00a0<code>n<\/code>\u00a0nodes, the middle is at position\u00a0<code>n\/2<\/code><\/li>\n\n\n\n<li>Fast pointer moves 2 steps per iteration, slow pointer moves 1 step per iteration<\/li>\n\n\n\n<li>After\u00a0<code>k<\/code>\u00a0iterations: fast pointer is at position\u00a0<code>2k<\/code>, slow pointer is at position\u00a0<code>k<\/code><\/li>\n\n\n\n<li>When fast pointer reaches the end (<code>2k = n<\/code>), slow pointer is at\u00a0<code>k = n\/2<\/code>, which is the middle<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Handling different cases:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Odd length:<\/strong>\u00a0Fast pointer reaches the last node, slow pointer is at exact middle<\/li>\n\n\n\n<li><strong>Even length:<\/strong>\u00a0Fast pointer goes beyond the list, slow pointer is at the second middle node<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why is this optimal?<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Single pass:<\/strong>\u00a0We only traverse the list once<\/li>\n\n\n\n<li><strong>No counting needed:<\/strong>\u00a0We don&#8217;t need to know the total length beforehand<\/li>\n\n\n\n<li><strong>Constant space:<\/strong>\u00a0Only using two pointers<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>When should you use this approach?<\/strong><br>This is the preferred method for interviews and production code because it&#8217;s efficient and demonstrates good understanding of pointer manipulation.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/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 middleNode(self, head: ListNode) -&gt; ListNode:\n        slow = head                         # slow pointer starts at head\n        fast = head                         # fast pointer starts at head\n        \n        # Move until fast pointer reaches the end\n        while fast and fast.next:\n            slow = slow.next                # slow moves 1 step\n            fast = fast.next.next           # fast moves 2 steps\n        \n        return slow                         # slow is now at the middle\" 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\">middleNode<\/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\">: ListNode) -&gt; ListNode:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        slow = head                         <\/span><span style=\"color: #6A9955\"># slow pointer starts at head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        fast = head                         <\/span><span style=\"color: #6A9955\"># fast pointer starts at head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Move until fast pointer reaches the end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> fast <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> fast.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            slow = slow.next                <\/span><span style=\"color: #6A9955\"># slow moves 1 step<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            fast = fast.next.next           <\/span><span style=\"color: #6A9955\"># fast moves 2 steps<\/span><\/span>\n<span class=\"line\"><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\"> slow                         <\/span><span style=\"color: #6A9955\"># slow is now at the middle<\/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=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p><strong>Step 1: Initialize pointers<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Both\u00a0<code>slow<\/code>\u00a0and\u00a0<code>fast<\/code>\u00a0pointers start at the head of the list<\/li>\n\n\n\n<li>This ensures they both begin the race from the same starting point<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: The main loop condition<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>while fast and fast.next:<\/code>\u00a0ensures we don&#8217;t go out of bounds<\/li>\n\n\n\n<li><code>fast<\/code>\u00a0checks if the current node exists<\/li>\n\n\n\n<li><code>fast.next<\/code>\u00a0checks if the next node exists (needed since fast moves 2 steps)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: Move pointers<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>slow = slow.next<\/code>: slow pointer moves 1 step forward<\/li>\n\n\n\n<li><code>fast = fast.next.next<\/code>: fast pointer moves 2 steps forward<\/li>\n\n\n\n<li>This happens in each iteration until fast reaches the end<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4: Return result<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When the loop ends,\u00a0<code>slow<\/code>\u00a0is pointing to the middle node<\/li>\n\n\n\n<li>We return\u00a0<code>slow<\/code>\u00a0as the answer<\/li>\n<\/ul>\n\n\n\n<p><strong>Why the loop condition works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Odd length:<\/strong>\u00a0Fast pointer will be at the last node, and fast.next will be None, so loop stops<\/li>\n\n\n\n<li><strong>Even length:<\/strong>\u00a0Fast pointer will be None (went past the end), so loop stops<\/li>\n\n\n\n<li>In both cases, slow pointer ends up at the correct middle position<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through this with two examples:<\/p>\n\n\n\n<p><strong>Example 1 (Odd length):<\/strong><br><code>head = [1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Initial:<\/strong>\u00a0slow = 1, fast = 1<\/li>\n\n\n\n<li><strong>Iteration 1:<\/strong>\u00a0slow = 2, fast = 3<\/li>\n\n\n\n<li><strong>Iteration 2:<\/strong>\u00a0slow = 3, fast = 5<\/li>\n\n\n\n<li><strong>Check condition:<\/strong>\u00a0fast = 5 (exists), fast.next = None (doesn&#8217;t exist)<\/li>\n\n\n\n<li><strong>Loop ends:<\/strong>\u00a0Return slow = 3<\/li>\n<\/ul>\n\n\n\n<p><strong>Example 2 (Even length):<\/strong><br><code>head = [1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Initial:<\/strong>\u00a0slow = 1, fast = 1<\/li>\n\n\n\n<li><strong>Iteration 1:<\/strong>\u00a0slow = 2, fast = 3<\/li>\n\n\n\n<li><strong>Iteration 2:<\/strong>\u00a0slow = 3, fast = 5<\/li>\n\n\n\n<li><strong>Iteration 3:<\/strong>\u00a0slow = 4, fast = None (went past the end)<\/li>\n\n\n\n<li><strong>Check condition:<\/strong>\u00a0fast = None (doesn&#8217;t exist)<\/li>\n\n\n\n<li><strong>Loop ends:<\/strong>\u00a0Return slow = 4<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n\/2) = O(n)<br>We traverse at most half the list (when fast pointer reaches the end)<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only using two pointer variables<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution using two pointers is elegant and efficient. It demonstrates a deep understanding of linked list traversal and is the preferred approach for coding interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Middle of the Linked List&#8221; problem is an excellent way to learn about linked list traversal techniques:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start with the brute force approach<\/strong>\u00a0to understand the fundamentals<\/li>\n\n\n\n<li><strong>Master the two-pointer technique<\/strong>\u00a0for optimal performance and to impress in interviews<\/li>\n\n\n\n<li><strong>The two-pointer approach is a powerful pattern<\/strong>\u00a0that appears in many linked list problems<\/li>\n<\/ul>\n\n\n\n<p>Understanding both approaches will make you a stronger problem solver and help you tackle more complex linked list challenges.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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<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>The &#8220;Middle of the Linked List&#8221; problem is a classic linked list question that helps you understand traversal techniques and the famous two-pointer approach. In this blog, we&#8217;ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with detailed explanations, code comments, dry runs, and clear<\/p>\n","protected":false},"author":1,"featured_media":631,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[8,29],"class_list":{"0":"post-628","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-singly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/middle-of-the-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\/628","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=628"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions\/633"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/631"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}