{"id":634,"date":"2025-07-15T12:33:11","date_gmt":"2025-07-15T07:03:11","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=634"},"modified":"2025-07-15T12:33:13","modified_gmt":"2025-07-15T07:03:13","slug":"reverse-linked-list-leetcode-206","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/","title":{"rendered":"Reverse Linked List | Leetcode 206 | Iterative vs Recursive Solution"},"content":{"rendered":"\n<p>The &#8220;Reverse Linked&nbsp;List&#8221; problem&nbsp;is one of the&nbsp;most fundamental&nbsp;and frequently&nbsp;asked problems&nbsp;in data structures. It tests your&nbsp;understanding&nbsp;of pointer manipulation&nbsp;and recursion&nbsp;in a singly linked&nbsp;list. <\/p>\n\n\n\n<p>In this&nbsp;blog, we\u2019ll walk&nbsp;through three&nbsp;detailed approaches: brute force, iterative, and&nbsp;recursive. We\u2019ll explain the&nbsp;intuition, approach, and provide&nbsp;an easy-to-understand code&nbsp;explanation for&nbsp;each method.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/reverse-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-b688a6f7-8827-4c61-ad00-dcb5397386a7\" 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\/reverse-linked-list-leetcode-206\/#0-what-does-the-problem-say\" style=\"\">What Does the Problem Say?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#1-solution-1-brute-force-approach-using-stack\" style=\"\">Solution 1: Brute Force Approach (Using Stack)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#2-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#3-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#4-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#5-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#6-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#7-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#8-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#9-solution-2-iterative-approach-optimal\" style=\"\">Solution 2: Iterative Approach (Optimal)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#10-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#11-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#12-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#16-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#17-solution-3-recursive-approach\" style=\"\">Solution 3: Recursive Approach<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#18-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#19-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#20-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#21-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#22-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#23-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#24-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-linked-list-leetcode-206\/#25-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-what-does-the-problem-say\">What Does the Problem Say?<\/h2>\n\n\n\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a singly linked list, reverse the list, and return&nbsp;<em>the reversed list<\/em>.<\/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\/02\/19\/rev1ex1.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> [5,4,3,2,1]<\/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\/02\/19\/rev1ex2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1,2]<br><strong>Output:<\/strong> [2,1]<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = []<br><strong>Output:<\/strong> []<\/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 the range&nbsp;<code>[0, 5000]<\/code>.<\/li>\n\n\n\n<li><code>-5000 &lt;= Node.val &lt;= 5000<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;A linked list can be reversed either iteratively or recursively. Could you implement both?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-solution-1-brute-force-approach-using-stack\">Solution 1: Brute Force Approach (Using Stack)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like stacking plates! When you put plates on a stack, the last plate you put becomes the first one you take out. We&#8217;ll use this same idea &#8211; store all the values in a stack, then put them back in reverse order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>First Pass<\/strong>: Walk through the entire linked list and store all node values in a stack<\/li>\n\n\n\n<li><strong>Second Pass<\/strong>: Walk through the linked list again and replace each node&#8217;s value with values popped from the stack<\/li>\n\n\n\n<li>Since stack follows LIFO (Last In, First Out), we get the reverse order automatically<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-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 reverseList(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        temp = head\n        stack = []\n        \n        # First pass: Store all values in stack\n        while temp is not None:\n            stack.append(temp.val)  # Push current node's value to stack\n            temp = temp.next\n        \n        temp = head  # Reset temp to head for second pass\n        \n        # Second pass: Pop values from stack and update nodes\n        while temp is not None:\n            temp.val = stack.pop()  # Replace current value with stack's top\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\">reverseList<\/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\">        temp = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        stack = []<\/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\"># First pass: Store all values in stack<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/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\">            stack.append(temp.val)  <\/span><span style=\"color: #6A9955\"># Push current node&#39;s value to stack<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head  <\/span><span style=\"color: #6A9955\"># Reset temp to head for second pass<\/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\"># Second pass: Pop values from stack and update 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 style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/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\">            temp.val = stack.pop()  <\/span><span style=\"color: #6A9955\"># Replace current value with stack&#39;s top<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next<\/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\"> 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=\"5-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use two separate loops here. In the first loop, we visit every node and push its value onto our stack. This gives us all values stored in reverse order (since stack is LIFO). In the second loop, we visit each node again but this time we pop values from the stack and update each node&#8217;s value. Since we&#8217;re popping in reverse order, we effectively reverse the entire list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3<\/code><\/p>\n\n\n\n<p><strong>First Pass:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visit node 1: stack =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li>Visit node 2: stack =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li>Visit node 3: stack =&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>Second Pass:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visit node 1: pop 3, node becomes 3:&nbsp;<code>3 -&gt; 2 -&gt; 3<\/code><\/li>\n\n\n\n<li>Visit node 2: pop 2, node becomes 2:&nbsp;<code>3 -&gt; 2 -&gt; 3<\/code><\/li>\n\n\n\n<li>Visit node 3: pop 1, node becomes 1:&nbsp;<code>3 -&gt; 2 -&gt; 1<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>3 -&gt; 2 -&gt; 1<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-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 twice<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(n) &#8211; We store all values in the stack<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is easy to understand but uses extra space. We&#8217;re basically copying all values to a temporary storage (stack) and then putting them back in reverse order.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-solution-2-iterative-approach-optimal\">Solution 2: Iterative Approach (Optimal)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-intuition\">Intuition<\/h3>\n\n\n\n<p>Imagine you&#8217;re holding hands in a line and want to face the opposite direction. Instead of everyone turning around, you can change who&#8217;s holding whose hand! Each person just needs to hold the hand of the person who was previously behind them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Track Three Pointers<\/strong>: Keep track of previous node, current node, and next node<\/li>\n\n\n\n<li><strong>Reverse the Link<\/strong>: Make current node point to the previous node instead of the next node<\/li>\n\n\n\n<li><strong>Move Forward<\/strong>: Shift all pointers one step forward<\/li>\n\n\n\n<li><strong>Repeat<\/strong>: Continue until we reach the end of the list<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-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 reverseList(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        temp = head    # Current node we're processing\n        prev = None    # Previous node (starts as None)\n        \n        while temp is not None:\n            front = temp.next     # Store next node before we lose it\n            temp.next = prev      # Reverse the link: point to previous\n            prev = temp           # Move prev pointer forward\n            temp = front          # Move current pointer forward\n        \n        return prev  # prev is now the new head of reversed list\" 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\">reverseList<\/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\">        temp = head    <\/span><span style=\"color: #6A9955\"># Current node we&#39;re processing<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        prev = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Previous node (starts as None)<\/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\">while<\/span><span style=\"color: #D4D4D4\"> temp <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/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\">            front = temp.next     <\/span><span style=\"color: #6A9955\"># Store next node before we lose it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp.next = prev      <\/span><span style=\"color: #6A9955\"># Reverse the link: point to previous<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            prev = temp           <\/span><span style=\"color: #6A9955\"># Move prev pointer forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = front          <\/span><span style=\"color: #6A9955\"># Move current pointer forward<\/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\"> prev  <\/span><span style=\"color: #6A9955\"># prev is now the new head of reversed list<\/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=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use three pointers to carefully reverse each link. The&nbsp;<code>front<\/code>&nbsp;pointer helps us remember where to go next before we break the current connection. We reverse each arrow one by one, making sure not to lose track of where we are in the list. By the end,&nbsp;<code>prev<\/code>&nbsp;points to what used to be the last node, which is now our new first node.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3<\/code><\/p>\n\n\n\n<p><strong>Initial:<\/strong>&nbsp;temp=1, prev=None<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>None &lt;- 1 -&gt; 2 -&gt; 3<br>       ^    ^<br>     prev  temp<\/code><\/pre>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;front=2, reverse link, move pointers<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>None &lt;- 1    2 -&gt; 3<br>            ^    ^<br>          prev  temp<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;front=3, reverse link, move pointers<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>None &lt;- 1 &lt;- 2    3<br>                 ^    ^<br>               prev  temp<\/code><\/pre>\n\n\n\n<p><strong>Step 3:<\/strong>&nbsp;front=None, reverse link, move pointers<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>None &lt;- 1 &lt;- 2 &lt;- 3    None<br>                  ^      ^<br>                prev   temp<\/code><\/pre>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;Return prev (which points to 3)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-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 visit each node exactly 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=\"16-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This is the most efficient approach! We change the direction of arrows one by one without needing any extra storage space. Just like redirecting traffic flow on a one-way street.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-solution-3-recursive-approach\">Solution 3: Recursive Approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like a chain of people passing a message backwards. Each person tells the person behind them to reverse their part of the chain, then fixes their own connection. It&#8217;s like solving smaller versions of the same problem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Base Case<\/strong>: If we reach the end (or empty list), stop the recursion<\/li>\n\n\n\n<li><strong>Recursive Call<\/strong>: Ask the rest of the list to reverse itself<\/li>\n\n\n\n<li><strong>Fix Current Connection<\/strong>: Once the rest is reversed, fix the current node&#8217;s connection<\/li>\n\n\n\n<li><strong>Return New Head<\/strong>: The deepest recursive call found the new head, keep passing it back<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-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 reverseList(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        # Base case: empty list or single node\n        if head is None or head.next is None:\n            return head\n        \n        # Recursively reverse the rest of the list\n        new_head = self.reverseList(head.next)\n        \n        # Fix the current connection\n        front = head.next          # The node that comes after current\n        front.next = head          # Make it point back to current\n        head.next = None           # Remove current's forward connection\n        \n        return new_head  # Return the head of the reversed list\" 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\">reverseList<\/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: #6A9955\"># Base case: empty list or single node<\/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>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Recursively reverse the rest of the list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        new_head = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverseList(head.next)<\/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\"># Fix the current connection<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        front = head.next          <\/span><span style=\"color: #6A9955\"># The node that comes after current<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        front.next = head          <\/span><span style=\"color: #6A9955\"># Make it point back to current<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        head.next = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># Remove current&#39;s forward connection<\/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\"> new_head  <\/span><span style=\"color: #6A9955\"># Return the head of the reversed list<\/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=\"21-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>The recursion goes deep until it finds the last node, which becomes the new head. As the recursive calls return, each level fixes its own connection by making the next node point backwards. It&#8217;s like a domino effect in reverse &#8211; each piece fixes its connection as the solution bubbles back up.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3<\/code><\/p>\n\n\n\n<p><strong>Call Stack:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>reverseList(1): calls reverseList(2)<br>  reverseList(2): calls reverseList(3)<br>    reverseList(3): returns 3 (base case)<br>  reverseList(2): new_head=3, fix 3-&gt;2, return 3<br>reverseList(1): new_head=3, fix 2-&gt;1, return 3<\/code><\/pre>\n\n\n\n<p><strong>Step by step:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>reverseList(3)<\/code>&nbsp;returns 3 (base case)<\/li>\n\n\n\n<li><code>reverseList(2)<\/code>&nbsp;gets new_head=3, makes 3 point to 2:&nbsp;<code>1 -&gt; 2 &lt;- 3<\/code><\/li>\n\n\n\n<li><code>reverseList(1)<\/code>&nbsp;gets new_head=3, makes 2 point to 1:&nbsp;<code>None &lt;- 1 &lt;- 2 &lt;- 3<\/code><\/li>\n<\/ol>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;Returns 3 as the new head<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-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 visit each node exactly once<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(n) &#8211; Due to recursion call stack (n recursive calls)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"24-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is elegant but uses extra memory for the call stack. Each recursive call waits for the next one to finish, like a stack of function calls. It&#8217;s beautiful conceptually but not the most memory-efficient for large lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"25-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<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Easy<\/td><td>Learning concepts<\/td><\/tr><tr><td>Iterative<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><tr><td>Recursive<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Medium<\/td><td>Interview discussions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>iterative approach<\/strong>&nbsp;is typically the best choice as it&#8217;s efficient in both time and space. However, understanding all three approaches helps you tackle similar problems with different constraints!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Reverse Linked&nbsp;List&#8221; problem&nbsp;is one of the&nbsp;most fundamental&nbsp;and frequently&nbsp;asked problems&nbsp;in data structures. It tests your&nbsp;understanding&nbsp;of pointer manipulation&nbsp;and recursion&nbsp;in a singly linked&nbsp;list. In this&nbsp;blog, we\u2019ll walk&nbsp;through three&nbsp;detailed approaches: brute force, iterative, and&nbsp;recursive. We\u2019ll explain the&nbsp;intuition, approach, and provide&nbsp;an easy-to-understand code&nbsp;explanation for&nbsp;each method. Here&#8217;s the [Problem Link] to begin with. What Does the Problem Say? Given the&nbsp;head&nbsp;of<\/p>\n","protected":false},"author":1,"featured_media":635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[8,29],"class_list":{"0":"post-634","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\/reverse-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\/634","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=634"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions\/637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/635"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}