<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Rotate List ### Source - leetcode: [Rotate List | LeetCode OJ](https://leetcode.com/problems/rotate-list/) - lintcode: [(170) Rotate List](http://www.lintcode.com/en/problem/rotate-list/) ### Problem Given a list, rotate the list to the right by *k* places, where *k* is non-negative. #### Example Given `1->2->3->4->5` and k = `2`, return `4->5->1->2->3`. ### 題解 旋轉鏈表,鏈表類問題通常需要找到需要處理節點處的前一個節點。因此我們只需要找到旋轉節點和最后一個節點即可。需要注意的細節是 k 有可能比鏈表長度還要大,此時需要取模,另一個 corner case 則是鏈表長度和 k 等長。 ### Java ~~~ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /** * @param head: the List * @param k: rotate to the right k places * @return: the list after rotation */ public ListNode rotateRight(ListNode head, int k) { if (head == null) return head; ListNode fast = head, slow = head; int len = 1; for (len = 1; fast.next != null && len <= k; len++) { fast = fast.next; } // k mod len if k > len if (len <= k) { k = k % len; fast = head; for (int i = 0; i < k; i++) { fast = fast.next; } } // forward slow and fast while (fast.next != null) { fast = fast.next; slow = slow.next; } // return new head fast.next = head; head = slow.next; slow.next = null; return head; } } ~~~ ### 源碼分析 由于需要處理的是節點的前一個節點,故最終的`while` 循環使用`fast.next != null`. k 與鏈表等長時包含在`len <= k`中。 ### 復雜度分析 時間復雜度 O(n)O(n)O(n), 空間復雜度 O(1)O(1)O(1).
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看