LeetCode[Linked list]: Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:

Given
1->2->3->4->5->NULL
and
k = 2
,

return
4->5->1->2->3->NULL
.

看到这个题目感到奇怪的是为什么是“右旋”,而不是“左旋”呢?那么,左旋与右旋有什么区别呢?首先说一下我的解题思路:

首先把链表的头和尾连接起来成为一个环,然后通过指针的移动来实现旋转,旋转到正确的位置之后,将环断开成链表再返回。

按照这个思路,“左旋”确实要简单很多,因为该链表是单向链表,而不是双向链表。左旋代码实现如下:

C++ code

    ListNode *rotateLeft(ListNode *head, int k) {
        if (!head) return NULL;

        ListNode *curr = head;
        while (curr->next) curr = curr->next; // 该循环找到尾节点
        curr->next = head; // 此时curr指向尾节点,将尾连接至头

        for (int i = 0; i < k; ++i)
            curr = curr->next; // 左旋k步

        head = curr->next; // 将环断开成为链表
        curr->next = NULL;

        return head;
    }

那么如果按照这种思路来实现右旋呢?一种投机取巧的方法就是右旋k步其实就是左旋count
- k % count
(count是指整个链表的长度)。代码实现如下:

C++ code

    ListNode *rotateRight(ListNode *head, int k) {
        if (!head) return head;
        if (k ==0) return head;

        int count = 1;
        ListNode *curr;
        for (curr = head; curr->next != NULL; curr = curr->next)
            ++count;
        curr->next = head;

        for (int i = 0; i < count - k % count; ++i)
            curr = curr->next;

        head = curr->next;
        curr->next = NULL;

        return head;
    }

上面算法的时间复杂度均为O(N),空间复杂度为O(1),可以采取一定的手段牺牲空间性能去掉第二个循环来换取一定的时间性能:

C++ code

    ListNode *rotateRight(ListNode *head, int k) {
        if (!head ) return head;
        if (k == 0) return head;

        vector<ListNode *> nodeVec;
        for (ListNode *curr = head; curr != NULL; curr = curr->next)
            nodeVec.push_back(curr);

        int listLen = nodeVec.size();
        k %= listLen;
        if (k == 0) return head;
        nodeVec[listLen - 1]->next = nodeVec[0];

        nodeVec[listLen - k - 1]->next = NULL;
        return nodeVec[listLen - k];
    }

无论是左旋还是右旋,均可通过上面的方式来实现。空间复杂度变为O(N),时间复杂度仍为O(N)。

以上是我独立思考的结果。看到Discuss上讨论比较多的是设置两个指针的做法,按照这个思路,我做了如下实现:

时间: 2024-10-11 09:25:04

LeetCode[Linked list]: Rotate List的相关文章

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

LeetCode: Linked List Cycle [141]

[题目] Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? [题意] 判断一个单向链表是否有环 [思路] 维护两个指针p1和p2,p1每次向前移动一步,p2每次向前移动两步 如果p2能够追上p1,则说明链表中存在环 [代码] /** * Definition for singly-linked list. * struct L

LeetCode: Linked List Cycle II [142]

[题目] Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

leetcode --- Linked List Cycle [Floyd&#39;s cycle-finding algorithm]

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Two Pointers ''' Created on Nov 13, 2014 @author: ScottGu<[email protected], [email protected]> ''' # Definit

Leetcode:Linked List Cycle 链表是否存在环

Linked List Cycle: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解题分析: 大致思想就是设置两个指针,一个指针每次走两步,一个指针每次走一步,如果这两个指针碰头了,那么一定就存在环 可以类比两个人在环形操场跑步,同时出发,一个跑得快,一个跑得慢,如果跑得快的人追上跑得慢的人,那么跑得快的人相当于多跑了一整

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

LeetCode[Linked List]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个题目跟Linked List Cycle一样,我也没有能够自己独立找到解决方法,而是借助Discuss学到了一个非常巧妙的解决方法: 首先借助LeetCode[Linked List]: Lin

[LeetCode]Linked List Cycle II解法学习

问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题. 通过反复思考,我觉得这题类似于追击问题,可以用一个

[LeetCode]Linked List Cycle

题目:Linked List Cycle 判断一个单链表中是否有环,要求常量空间复杂度: 思路: 使用两个指针同时从链表表头开始移动,一个移动一步,一个移动两步,直到两个指针重合或某一指针指向链尾. 两个指针重合则单链表有环存在,否则没有. 第二个指针以第一个指针的两倍的速度移动,而第一个指针每次移动一步,这样只要有环,两个指针必定能重合. bool LeetCode::hasCycle(ListNode *head){ if (!head)return false; ListNode *p =