{"id":7573,"date":"2020-10-28T22:59:44","date_gmt":"2020-10-29T05:59:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7573"},"modified":"2020-10-29T20:35:05","modified_gmt":"2020-10-30T03:35:05","slug":"leetcode-61-rotate-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-61-rotate-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 61. Rotate List"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 61. Rotate List - \u5237\u9898\u627e\u5de5\u4f5c EP365\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/a4XZu2VVE9Q?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a linked&nbsp;list, rotate the list to the right by&nbsp;<em>k<\/em>&nbsp;places, where&nbsp;<em>k<\/em>&nbsp;is non-negative.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, k = 2\n<strong>Output:<\/strong> 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL\n<strong>Explanation:<\/strong>\nrotate 1 steps to the right: 5-&gt;1-&gt;2-&gt;3-&gt;4-&gt;NULL\nrotate 2 steps to the right: 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 0-&gt;1-&gt;2-&gt;NULL, k = 4\n<strong>Output:<\/strong> <code>2-&gt;0-&gt;1-&gt;NULL<\/code>\n<strong>Explanation:<\/strong>\nrotate 1 steps to the right: 2-&gt;0-&gt;1-&gt;NULL\nrotate 2 steps to the right: 1-&gt;2-&gt;0-&gt;NULL\nrotate 3 steps to the right:&nbsp;<code>0-&gt;1-&gt;2-&gt;NULL<\/code>\nrotate 4 steps to the right:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Find the prev of the new head<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/61-ep365.png\" alt=\"\" class=\"wp-image-7576\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/61-ep365.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/61-ep365-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/61-ep365-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Step 1: Get the tail node T while counting the length of the list.<br>Step 2: k %= l, k can be greater than l, rotate k % l times has the same effect.<br>Step 3: Find the previous node P of the new head N by moving (l &#8211; k &#8211; 1) steps from head<br>Step 4: set P.next to null, T.next to head and return N<\/p>\n\n\n\n<p>Time complexity: O(n) n is the length of the list<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* rotateRight(ListNode* head, int k) {\n    if (!head) return head;    \n    int l = 1;\n    ListNode* tail = head;\n    while (tail->next) { tail = tail->next; ++l; }\n    k %= l;\n    if (k == 0) return head;\n    \n    ListNode* prev = head;\n    while (--l > k) prev = prev->next;\n    ListNode* new_head = prev->next;\n    tail->next = head;\n    prev->next = nullptr;\n    return new_head;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n *\/\nclass Solution {\n  public ListNode rotateRight(ListNode head, int k) {\n    if (head == null) return null;\n    int l = 1;\n    ListNode tail = head;\n    while (tail.next != null) {\n      tail = tail.next;\n      ++l;\n    }\n    k %= l;\n    if (k == 0) return head;\n    \n    ListNode prev = head;\n    for (int i = 0; i < l - k - 1; ++i) {\n      prev = prev.next;\n    }\n    \n    ListNode new_head = prev.next;\n    prev.next = null;\n    tail.next = head;\n    return new_head;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def rotateRight(self, head: ListNode, k: int) -> ListNode:\n    if not head: return head\n    tail = head\n    l = 1\n    while tail.next: \n      tail = tail.next\n      l += 1\n    k = k % l\n    if k == 0: return head\n    \n    prev = head\n    for _ in range(l - k - 1): prev = prev.next\n    \n    new_head = prev.next\n    tail.next = head\n    prev.next = None\n    return new_head\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a linked&nbsp;list, rotate the list to the right by&nbsp;k&nbsp;places, where&nbsp;k&nbsp;is non-negative. Example 1: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, k = 2 Output: 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL Explanation: rotate 1 steps&#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":[83,177,412,250],"class_list":["post-7573","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-node","tag-rotate","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7573","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=7573"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7573\/revisions"}],"predecessor-version":[{"id":7578,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7573\/revisions\/7578"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}