题目描述

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

  • 地址:https://leetcode-cn.com/problems/rotate-list/
  • 2021/03/27

    法一:链表闭合成环

  • 若head null 或 head->next null 直接就返回

  • 若 k = 0 或 k 为 n(链表的 长度) 的倍数,直接返回原链表
  • 新链表的最后一个节点为原链表的第 (n - 1) - (k mod n)个节点(从 0 开始计数)

    • 找到新链表的最后一个节点,将链表断开
  • 时间复杂度 O(n)

  • 空间复杂度 O(1)

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* rotateRight(ListNode* head, int k) {
    14. if (head == nullptr || head->next == nullptr || k == 0) return head;
    15. int n = 1;
    16. ListNode* tail = head;
    17. while (tail->next != nullptr) { // 统计链表长度
    18. tail = tail->next;
    19. n ++;
    20. }
    21. int tmp = n - (k % n);
    22. if (tmp == n) return head; // n 为 k 的倍数
    23. tail->next = head; // 闭合
    24. while (tmp --) { // 判断断开的位置
    25. tail = tail->next;
    26. }
    27. ListNode* ans = tail->next;
    28. tail->next = nullptr;
    29. return ans;
    30. }
    31. };
    1. # Definition for singly-linked list.
    2. # class ListNode(object):
    3. # def __init__(self, val=0, next=None):
    4. # self.val = val
    5. # self.next = next
    6. class Solution(object):
    7. def rotateRight(self, head, k):
    8. if not head or not head.next or k == 0:
    9. return head;
    10. n = 1
    11. tail = head
    12. while tail.next:
    13. tail = tail.next
    14. n += 1
    15. tmp = n - k % n
    16. if tmp == n:
    17. return head
    18. tail.next = head
    19. while tmp:
    20. tail = tail.next
    21. tmp -= 1
    22. ans = tail.next
    23. tail.next = None
    24. return ans

    法二:快慢指针

  • 用「快慢指针」找到倒数第 k 个节点即新的链表头

  • 时间复杂度 O(n)

  • 空间复杂度 O(1)

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* rotateRight(ListNode* head, int k) {
    14. if (head == nullptr || head->next == nullptr || k == 0) return head;
    15. int n = 1;
    16. ListNode* cur = head;
    17. while (cur->next != nullptr) { // 统计链表的长度
    18. n ++;
    19. cur = cur->next;
    20. }
    21. k %= n;
    22. if (k == 0) return head;
    23. // 快慢指针遍历链表,slow会停在new head的前一位
    24. ListNode* slow = head;
    25. ListNode* fast = head;
    26. while (k-- > 0) fast = fast->next; // fast 此时与slow 相隔 k,即fast 刚好是 slow 旋转 k 后的位置
    27. while (fast->next != nullptr) { // 同步往前走,即反转链表的前半部分
    28. slow = slow->next;
    29. fast = fast->next;
    30. }
    31. ListNode* newHead = slow->next;
    32. slow->next = nullptr;
    33. fast->next = head; // 将新链表的前半部分与后半部分拼接
    34. return newHead;
    35. }
    36. };
    1. # Definition for singly-linked list.
    2. # class ListNode(object):
    3. # def __init__(self, val=0, next=None):
    4. # self.val = val
    5. # self.next = next
    6. class Solution(object):
    7. def rotateRight(self, head, k):
    8. if not head or not head.next or k == 0:
    9. return head
    10. n = 1
    11. cur = head
    12. while cur.next != None:
    13. cur = cur.next
    14. n += 1
    15. k %= n
    16. if k == 0:
    17. return head
    18. slow = head
    19. fast = head
    20. while k > 0:
    21. fast = fast.next
    22. k -= 1
    23. while fast.next != None:
    24. slow = slow.next
    25. fast = fast.next
    26. newHead = slow.next
    27. slow.next = None
    28. fast.next = head
    29. return newHead