<?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>insertion Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/insertion/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/insertion/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Nov 2021 06:49:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>insertion Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/insertion/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 147. Insertion Sort List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-147-insertion-sort-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-147-insertion-sort-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Nov 2021 06:48:33 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[insertion]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8922</guid>

					<description><![CDATA[<p>Given the&#160;head&#160;of a singly linked list, sort the list using&#160;insertion sort, and return&#160;the sorted list&#8217;s head. The steps of the&#160;insertion sort&#160;algorithm: Insertion sort iterates, consuming&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-147-insertion-sort-list/">花花酱 LeetCode 147. Insertion Sort List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given the&nbsp;<code>head</code>&nbsp;of a singly linked list, sort the list using&nbsp;<strong>insertion sort</strong>, and return&nbsp;<em>the sorted list&#8217;s head</em>.</p>



<p>The steps of the&nbsp;<strong>insertion sort</strong>&nbsp;algorithm:</p>



<ol><li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li><li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li><li>It repeats until no input elements remain.</li></ol>



<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>



<figure class="wp-block-image"><img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" alt=""/></figure>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the list is in the range&nbsp;<code>[1, 5000]</code>.</li><li><code>-5000 &lt;= Node.val &lt;= 5000</code></li></ul>



<h2><strong>Solution: Scan from head</strong></h2>



<p>For each node, scan from head of the list to find the insertion position in O(n), and adjust pointers.</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode* insertionSortList(ListNode* head) {
    ListNode dummy;
    
    while (head) {
      ListNode* iter = &amp;dummy;
      while (iter-&gt;next &amp;&amp; head-&gt;val &gt; iter-&gt;next-&gt;val)
        iter = iter-&gt;next;
      ListNode* next = head-&gt;next;
      head-&gt;next = iter-&gt;next;
      iter-&gt;next = head;
      head = next;
    }
    
    return dummy.next;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-147-insertion-sort-list/">花花酱 LeetCode 147. Insertion Sort List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/list/leetcode-147-insertion-sort-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
