<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>recurrsion &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/recurrsion/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 05 Apr 2025 20:23:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>recurrsion &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 430. Flatten a Multilevel Doubly Linked List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-430-flatten-a-multilevel-doubly-linked-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-430-flatten-a-multilevel-doubly-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Apr 2025 20:20:38 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[doubly linked list]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[recurrsion]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10272</guid>

					<description><![CDATA[用递归实现比较简单一点。如果需要完美的one pass则需要写一个helper function，返回flattern后的head和tail. 注意这是双向链表，要把A把B连接到一起，需要同时改两个指针A->next = B, B->prev = A。 时间复杂度：O(n)空间复杂度：O(n) / stack [crayon-67f2293706dde202905196/]]]></description>
										<content:encoded><![CDATA[
<p>用递归实现比较简单一点。如果需要完美的one pass则需要写一个helper function，返回flattern后的head和tail.</p>



<p>注意这是双向链表，要把A把B连接到一起，需要同时改两个指针A->next = B, B->prev = A。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(n) / stack</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  Node* flatten(Node* head) {
    return solve(head).first;
  }
 private:
  // Flaten head, return new head and new tail.
  pair&lt;Node*, Node*&gt; solve(Node* head) {
    if (!head) return {nullptr, nullptr};
    Node dummy;
    Node* cur = &amp;dummy;
    cur-&gt;next = head;
    while (cur-&gt;next) {
      cur = cur-&gt;next;
      if (cur-&gt;child) {
        auto [h, t] = solve(cur-&gt;child);
        t-&gt;next = cur-&gt;next;
        h-&gt;prev = cur;
        if (cur-&gt;next) {
          cur-&gt;next-&gt;prev = t;
        }
        cur-&gt;next = h;
        cur-&gt;child = nullptr;
      }
    }
    return {dummy.next, cur};
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/list/leetcode-430-flatten-a-multilevel-doubly-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
