<?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>middle Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/middle/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/middle/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Dec 2021 06:24:48 +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>middle Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/middle/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2095. Delete the Middle Node of a Linked List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-2095-delete-the-middle-node-of-a-linked-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-2095-delete-the-middle-node-of-a-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Dec 2021 06:24:18 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[fast slow]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[middle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9020</guid>

					<description><![CDATA[<p>You are given the&#160;head&#160;of a linked list.&#160;Delete&#160;the&#160;middle node, and return&#160;the&#160;head&#160;of the modified linked list. The&#160;middle node&#160;of a linked list of size&#160;n&#160;is the&#160;⌊n / 2⌋th&#160;node from&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-2095-delete-the-middle-node-of-a-linked-list/">花花酱 LeetCode 2095. Delete the Middle Node of a Linked 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>You are given the&nbsp;<code>head</code>&nbsp;of a linked list.&nbsp;<strong>Delete</strong>&nbsp;the&nbsp;<strong>middle node</strong>, and return&nbsp;<em>the</em>&nbsp;<code>head</code>&nbsp;<em>of the modified linked list</em>.</p>



<p>The&nbsp;<strong>middle node</strong>&nbsp;of a linked list of size&nbsp;<code>n</code>&nbsp;is the&nbsp;<code>⌊n / 2⌋<sup>th</sup></code>&nbsp;node from the&nbsp;<strong>start</strong>&nbsp;using&nbsp;<strong>0-based indexing</strong>, where&nbsp;<code>⌊x⌋</code>&nbsp;denotes the largest integer less than or equal to&nbsp;<code>x</code>.</p>



<ul><li>For&nbsp;<code>n</code>&nbsp;=&nbsp;<code>1</code>,&nbsp;<code>2</code>,&nbsp;<code>3</code>,&nbsp;<code>4</code>, and&nbsp;<code>5</code>, the middle nodes are&nbsp;<code>0</code>,&nbsp;<code>1</code>,&nbsp;<code>1</code>,&nbsp;<code>2</code>, and&nbsp;<code>2</code>, respectively.</li></ul>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [1,3,4,7,1,2,6]
<strong>Output:</strong> [1,3,4,1,2,6]
<strong>Explanation:</strong>
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
We return the new list after removing this node. 
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,2,4]
<strong>Explanation:</strong>
The above figure represents the given linked list.
For n = 4, node 2 with value 3 is the middle node, which is marked in red.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [2,1]
<strong>Output:</strong> [2]
<strong>Explanation:</strong>
The above figure represents the given linked list.
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.</pre>



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



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



<h2><strong>Solution: Fast / Slow pointers</strong></h2>



<p>Use fast / slow pointers to find the previous node of the middle one, then skip the middle one.</p>



<p>prev.next = prev.next.next</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  ListNode* deleteMiddle(ListNode* head) {
    ListNode dummy(0, head);
    ListNode* prev = &amp;dummy;
    ListNode* fast = head;
    // prev points to the previous node of the middle one.
    while (fast &amp;&amp; fast-&gt;next) {
      prev = prev-&gt;next;
      fast = fast-&gt;next-&gt;next;
    }    
    prev-&gt;next = prev-&gt;next-&gt;next;
    return dummy.next;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-2095-delete-the-middle-node-of-a-linked-list/">花花酱 LeetCode 2095. Delete the Middle Node of a Linked 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-2095-delete-the-middle-node-of-a-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 876. Middle of the Linked List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-876-middle-of-the-linked-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-876-middle-of-the-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Jul 2018 04:35:52 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[middle]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3351</guid>

					<description><![CDATA[<p>Problem Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-876-middle-of-the-linked-list/">花花酱 LeetCode 876. Middle of the Linked 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[<h1>Problem</h1>
<p>Given a non-empty, singly linked list with head node <code>head</code>, return a middle node of linked list.</p>
<p>If there are two middle nodes, return the second middle node.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,2,3,4,5]</span>
<strong>Output: </strong>Node 3 from this list (Serialization: <span id="example-output-1">[3,4,5]</span>)
The returned node has value 3.  (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: </strong><span id="example-input-2-1">[1,2,3,4,5,6]</span>
<strong>Output: </strong>Node 4 from this list (Serialization: <span id="example-output-2">[4,5,6]</span>)
Since the list has two middle nodes with values 3 and 4, we return the second one.
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ul>
<li>The number of nodes in the given list will be between <code>1</code> and <code>100</code>.</li>
</ul>
<h1><strong>Solution: Slow + Fast Pointers</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  ListNode* middleNode(ListNode* head) {
    if (!head || !head-&gt;next) return head;
    auto slow = head;
    auto fast = head;
    while (fast &amp;&amp; fast-&gt;next) {
      slow = slow-&gt;next;
      fast = fast-&gt;next-&gt;next;
    }
    return slow;
  }
};</pre><p>&nbsp;</p>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-876-middle-of-the-linked-list/">花花酱 LeetCode 876. Middle of the Linked 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-876-middle-of-the-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
