<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Tracelit]]></title><description><![CDATA[Tracelit]]></description><link>https://blog.tracelit.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Tracelit</title><link>https://blog.tracelit.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 06:04:19 GMT</lastBuildDate><atom:link href="https://blog.tracelit.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[LeetCode 206: Reverse Linked List — Step-by-Step Visual Trace]]></title><description><![CDATA[The Problem
Given the head of a singly linked list, reverse the list and return the reversed list.
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
This is one of the most classic interview questions — simple enough to explain in 30 seconds, but...]]></description><link>https://blog.tracelit.dev/reverse-linked-list-visualization</link><guid isPermaLink="true">https://blog.tracelit.dev/reverse-linked-list-visualization</guid><dc:creator><![CDATA[tracelit]]></dc:creator><pubDate>Thu, 09 Apr 2026 03:10:51 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-the-problem">The Problem</h2>
<p>Given the head of a singly linked list, reverse the list and return the reversed list.</p>
<p><strong>Input:</strong> <code>head = [1, 2, 3, 4, 5]</code>
<strong>Output:</strong> <code>[5, 4, 3, 2, 1]</code></p>
<p>This is one of the most classic interview questions — simple enough to explain in 30 seconds, but tricky enough that getting the pointer manipulation right under pressure trips people up.</p>
<h2 id="heading-the-approach-three-pointers">The Approach: Three Pointers</h2>
<p>The iterative approach uses three pointers: <code>prev</code>, <code>current</code>, and <code>next_node</code>.</p>
<ol>
<li>Start with <code>prev = None</code> and <code>current = head</code></li>
<li>At each step:<ul>
<li>Save the next node: <code>next_node = current.next</code></li>
<li>Reverse the pointer: <code>current.next = prev</code></li>
<li>Move forward: <code>prev = current</code>, <code>current = next_node</code></li>
</ul>
</li>
<li>When <code>current</code> is <code>None</code>, <code>prev</code> points to the new head</li>
</ol>
<p><strong>Time:</strong> O(n) — single pass through the list
<strong>Space:</strong> O(1) — only three pointer variables</p>
<h2 id="heading-the-code">The Code</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">reverseList</span>(<span class="hljs-params">self, head: ListNode</span>) -&gt; ListNode:</span>
        prev = <span class="hljs-literal">None</span>
        current = head

        <span class="hljs-keyword">while</span> current:
            next_node = current.next
            current.next = prev
            prev = current
            current = next_node

        <span class="hljs-keyword">return</span> prev
</code></pre>
<h2 id="heading-watch-it-run">Watch It Run</h2>
<p>The best way to understand pointer manipulation is to <strong>see it happen</strong>. TraceLit traces your code line by line and shows you exactly how <code>prev</code>, <code>current</code>, and <code>next_node</code> move through the list at each step.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://tracelit.dev/app?trace=0206_reverse-linked-list">https://tracelit.dev/app?trace=0206_reverse-linked-list</a></div>
<blockquote>
<p><strong>Try it yourself:</strong> Open <a target="_blank" href="https://tracelit.dev/app?trace=0206_reverse-linked-list">TraceLit</a> and step through the trace. You can also paste your own solution and test it with different inputs.</p>
</blockquote>
<h2 id="heading-key-insight">Key Insight</h2>
<p>The trick is the <strong>order of operations</strong>. If you reverse <code>current.next = prev</code> before saving <code>current.next</code>, you lose the reference to the rest of the list. That's why <code>next_node = current.next</code> must come first.</p>
<p>This pattern — save, reverse, advance — shows up in many linked list problems:</p>
<ul>
<li><a target="_blank" href="https://leetcode.com/problems/reverse-linked-list-ii/">LeetCode 92: Reverse Linked List II</a></li>
<li><a target="_blank" href="https://leetcode.com/problems/reverse-nodes-in-k-group/">LeetCode 25: Reverse Nodes in k-Group</a></li>
<li><a target="_blank" href="https://leetcode.com/problems/palindrome-linked-list/">LeetCode 234: Palindrome Linked List</a></li>
</ul>
<hr />
<p><em>Built with <a target="_blank" href="https://tracelit.dev">TraceLit</a> — the visual algorithm tracer for LeetCode practice.</em></p>
]]></content:encoded></item></channel></rss>