<?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>dummy Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/dummy/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/dummy/</link>
	<description></description>
	<lastBuildDate>Tue, 02 Oct 2018 16:00:02 +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>dummy Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/dummy/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 24. Swap Nodes in Pairs</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 15:59:12 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[dummy]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[swap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4122</guid>

					<description><![CDATA[<p>Problem Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1-&#62;2-&#62;3-&#62;4, you should return the list as 2-&#62;1-&#62;4-&#62;3. Note: Your&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/">花花酱 LeetCode 24. Swap Nodes in Pairs</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><strong>Problem</strong></h1>
<p>Given a linked list, swap every two adjacent nodes and return its head.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">Given <code>1-&gt;2-&gt;3-&gt;4</code>, you should return the list as <code>2-&gt;1-&gt;4-&gt;3</code>.</pre>
<p><strong>Note:</strong></p>
<ul>
<li>Your algorithm should use only constant extra space.</li>
<li>You may <strong>not</strong> modify the values in the list&#8217;s nodes, only nodes itself may be changed.</li>
</ul>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode *swapPairs(ListNode *head) {
    if (!head || !head-&gt;next) return head;    

    ListNode d(0);
    d.next = head;
    head = &amp;d;

    while (head &amp;&amp; head-&gt;next &amp;&amp; head-&gt;next-&gt;next) {
      auto n1 = head-&gt;next;
      auto n2 = n1-&gt;next;

      n1-&gt;next = n2-&gt;next;
      n2-&gt;next = n1;

      head-&gt;next = n2;
      head = n1;
    }
    return d.next;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution:
  def swapPairs(self, head):
    if not head or not head.next: return head
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head.next and head.next.next:
      n1, n2 = head.next, head.next.next
      n1.next = n2.next
      n2.next = n1
      head.next = n2      
      head = n1
    return dummy.next</pre><p></div></div></p>
<h1>Related Problems</h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-148-sort-list/">花花酱 LeetCode 148. Sort List</a></li>
<li><a href="https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/">花花酱 LeetCode 203. Remove Linked List Elements</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/">花花酱 LeetCode 24. Swap Nodes in Pairs</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-24-swap-nodes-in-pairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
