返回介绍

Rotate List

发布于 2025-02-22 13:01:28 字数 1896 浏览 0 评论 0 收藏 0

Source

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文