{"id":4122,"date":"2018-10-02T08:59:12","date_gmt":"2018-10-02T15:59:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4122"},"modified":"2018-10-02T09:00:02","modified_gmt":"2018-10-02T16:00:02","slug":"leetcode-24-swap-nodes-in-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-24-swap-nodes-in-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 24. Swap Nodes in Pairs"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a\u00a0linked list, swap every two adjacent nodes and return its head.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<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>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Your algorithm should use only constant extra space.<\/li>\n<li>You may\u00a0<strong>not<\/strong>\u00a0modify the values in the list&#8217;s nodes, only nodes itself may be changed.<\/li>\n<\/ul>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  ListNode *swapPairs(ListNode *head) {\r\n    if (!head || !head-&gt;next) return head;    \r\n\r\n    ListNode d(0);\r\n    d.next = head;\r\n    head = &amp;d;\r\n\r\n    while (head &amp;&amp; head-&gt;next &amp;&amp; head-&gt;next-&gt;next) {\r\n      auto n1 = head-&gt;next;\r\n      auto n2 = n1-&gt;next;\r\n\r\n      n1-&gt;next = n2-&gt;next;\r\n      n2-&gt;next = n1;\r\n\r\n      head-&gt;next = n2;\r\n      head = n1;\r\n    }\r\n    return d.next;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">class Solution:\r\n  def swapPairs(self, head):\r\n    if not head or not head.next: return head\r\n    dummy = ListNode(0)\r\n    dummy.next = head\r\n    head = dummy\r\n    while head.next and head.next.next:\r\n      n1, n2 = head.next, head.next.next\r\n      n1.next = n2.next\r\n      n2.next = n1\r\n      head.next = n2      \r\n      head = n1\r\n    return dummy.next<\/pre>\n<\/div><\/div>\n<h1>Related Problems<\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/divide-and-conquer\/leetcode-148-sort-list\/\">\u82b1\u82b1\u9171 LeetCode 148. Sort List<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-203-remove-linked-list-elements\/\">\u82b1\u82b1\u9171 LeetCode 203. Remove Linked List Elements<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a\u00a0linked list, swap every two adjacent nodes and return its head. Example: Given 1-&gt;2-&gt;3-&gt;4, you should return the list as 2-&gt;1-&gt;4-&gt;3. Note: Your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[422,222,83,412,290],"class_list":["post-4122","post","type-post","status-publish","format-standard","hentry","category-list","tag-dummy","tag-easy","tag-list","tag-node","tag-swap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4122"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4122\/revisions"}],"predecessor-version":[{"id":4124,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4122\/revisions\/4124"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}