Leetcode: 【每日一题】- 2019-09-18 - 61. 旋转链表

Created on 18 Sep 2019  ·  4Comments  ·  Source: azl397985856/leetcode

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Daily Question Linked List

Most helpful comment

简单做法(修改指针指向):

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = 1;
        auto tail = head;
        while (tail->next != nullptr) {
            ++len;
            tail = tail->next;
        }
        k %= len;
        if (k == 0 || len == 1) return head;
        k = len - k;
        auto tmp = head;
        while (--k != 0) {
            tmp = tmp->next;
        }
        auto ret = tmp->next;
        tmp->next = nullptr;
        tail->next = head;
        return ret;
    }
};

脱裤子放屁法(反转,反转,再反转):

class Solution {
private:
    int length(ListNode* head) {
        auto cnt = 0;
        while (head != nullptr) {
            ++cnt;
            head = head->next;
        }
        return cnt;
    }
    ListNode* revert(ListNode* head, int n) {
        ListNode* prev = nullptr, *cur = head, *next = cur->next;
        while (--n != 0) {
            cur->next = prev;
            prev = cur;
            cur = next;
            next = next->next;
        }
        cur->next = prev;
        return cur;
    }
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = length(head);
        k = k % len;
        if (k == 0 || len == 1) return head;
        ListNode* p = head;
        auto tmp = len - k;
        while (tmp-- != 0) {
            p = p->next;
        }
        auto l1 = revert(head, len - k);
        auto l2 = revert(p, k);
        head->next = l2;
        return revert(l1, len);
    }
};

All 4 comments

简单做法(修改指针指向):

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = 1;
        auto tail = head;
        while (tail->next != nullptr) {
            ++len;
            tail = tail->next;
        }
        k %= len;
        if (k == 0 || len == 1) return head;
        k = len - k;
        auto tmp = head;
        while (--k != 0) {
            tmp = tmp->next;
        }
        auto ret = tmp->next;
        tmp->next = nullptr;
        tail->next = head;
        return ret;
    }
};

脱裤子放屁法(反转,反转,再反转):

class Solution {
private:
    int length(ListNode* head) {
        auto cnt = 0;
        while (head != nullptr) {
            ++cnt;
            head = head->next;
        }
        return cnt;
    }
    ListNode* revert(ListNode* head, int n) {
        ListNode* prev = nullptr, *cur = head, *next = cur->next;
        while (--n != 0) {
            cur->next = prev;
            prev = cur;
            cur = next;
            next = next->next;
        }
        cur->next = prev;
        return cur;
    }
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = length(head);
        k = k % len;
        if (k == 0 || len == 1) return head;
        ListNode* p = head;
        auto tmp = len - k;
        while (tmp-- != 0) {
            p = p->next;
        }
        auto l1 = revert(head, len - k);
        auto l2 = revert(p, k);
        head->next = l2;
        return revert(l1, len);
    }
};

简单做法(修改指针指向):

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = 1;
        auto tail = head;
        while (tail->next != nullptr) {
            ++len;
            tail = tail->next;
        }
        k %= len;
        if (k == 0 || len == 1) return head;
        k = len - k;
        auto tmp = head;
        while (--k != 0) {
            tmp = tmp->next;
        }
        auto ret = tmp->next;
        tmp->next = nullptr;
        tail->next = head;
        return ret;
    }
};

脱裤子放屁法(反转,反转,再反转):

class Solution {
private:
    int length(ListNode* head) {
        auto cnt = 0;
        while (head != nullptr) {
            ++cnt;
            head = head->next;
        }
        return cnt;
    }
    ListNode* revert(ListNode* head, int n) {
        ListNode* prev = nullptr, *cur = head, *next = cur->next;
        while (--n != 0) {
            cur->next = prev;
            prev = cur;
            cur = next;
            next = next->next;
        }
        cur->next = prev;
        return cur;
    }
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (head == nullptr || k <= 0) return head;
        auto len = length(head);
        k = k % len;
        if (k == 0 || len == 1) return head;
        ListNode* p = head;
        auto tmp = len - k;
        while (tmp-- != 0) {
            p = p->next;
        }
        auto l1 = revert(head, len - k);
        auto l2 = revert(p, k);
        head->next = l2;
        return revert(l1, len);
    }
};

rao 果然是大佬

rao 果然是大佬

伪大佬 #这个比字节跳动那个简单多了

Was this page helpful?
0 / 5 - 0 ratings

Related issues

azl397985856 picture azl397985856  ·  3Comments

azl397985856 picture azl397985856  ·  4Comments

azl397985856 picture azl397985856  ·  3Comments

azl397985856 picture azl397985856  ·  4Comments

azl397985856 picture azl397985856  ·  4Comments