{"id":4125,"date":"2018-10-02T09:27:56","date_gmt":"2018-10-02T16:27:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4125"},"modified":"2018-10-02T09:28:27","modified_gmt":"2018-10-02T16:28:27","slug":"leetcode-25-reverse-nodes-in-k-group","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-25-reverse-nodes-in-k-group\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 25. Reverse Nodes in k-Group"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a linked list, reverse the nodes of a linked list\u00a0<em>k<\/em>\u00a0at a time and return its modified list.<\/p>\n<p><em>k<\/em>\u00a0is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of\u00a0<em>k<\/em>\u00a0then left-out nodes in the end should remain as it is.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Given this linked list:\u00a0<code>1-&gt;2-&gt;3-&gt;4-&gt;5<\/code><\/p>\n<p>For\u00a0<em>k<\/em>\u00a0= 2, you should return:\u00a0<code>2-&gt;1-&gt;4-&gt;3-&gt;5<\/code><\/p>\n<p>For\u00a0<em>k<\/em>\u00a0= 3, you should return:\u00a0<code>3-&gt;2-&gt;1-&gt;4-&gt;5<\/code><\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Only constant extra memory is allowed.<\/li>\n<li>You may not alter 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>Two passes.<\/p>\n<p>First pass, get the length of the list.<\/p>\n<p>Second pass, swap in groups.<\/p>\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:default decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  ListNode *reverseKGroup(ListNode *head, int k) {\r\n    if (!head || k == 1) return head;\r\n    ListNode dummy(0);\r\n    dummy.next = head;\r\n    int len = 1;\r\n    while (head = head-&gt;next) len++;\r\n    ListNode* pre = &amp;dummy;    \r\n    for (int l = 0; l + k &lt;= len; l += k) {\r\n      ListNode* cur = pre-&gt;next;\r\n      ListNode* nxt = cur-&gt;next;\r\n      for (int i = 1; i &lt; k; ++i) {\r\n        cur-&gt;next = nxt-&gt;next;\r\n        nxt-&gt;next = pre-&gt;next;\r\n        pre-&gt;next = nxt;\r\n        nxt = cur-&gt;next;\r\n      }\r\n      pre = cur;\r\n    }\r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1>Related Problems<\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-24-swap-nodes-in-pairs\/\">\u82b1\u82b1\u9171 LeetCode 24. Swap Nodes in Pairs<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a linked list, reverse the nodes of a linked list\u00a0k\u00a0at a time and return its modified list. k\u00a0is a positive integer and is&#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":[217,83,290],"class_list":["post-4125","post","type-post","status-publish","format-standard","hentry","category-list","tag-hard","tag-list","tag-swap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4125","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=4125"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4125\/revisions"}],"predecessor-version":[{"id":4129,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4125\/revisions\/4129"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}