`
SunnyYoona
  • 浏览: 366560 次
社区版块
存档分类
最新评论

[LeetCode]61.Rotate List

 
阅读更多

【题目】

Given a list, rotate the list to the right bykplaces, wherekis non-negative.

For example:
Given1->2->3->4->5->NULLandk=2,
return4->5->1->2->3->NULL.

【题意】

给定一个链表,向右旋转k个位置,其中k是非负的。

【分析】

先遍历一遍,得出链表长度 len,注意 k 可能大于 len,因此令 k% = len。将尾节点 next 指针
指向首节点,形成一个环,接着往后跑 len - k 步,从这里断开,就是要求的结果了。

【代码1】

/*********************************
*   日期:2014-01-29
*   作者:SJF0115
*   题号: Rotate List
*   来源:http://oj.leetcode.com/problems/rotate-list/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(head == NULL || k <= 0){
            return head;
        }
        int count = 1;
        ListNode *pre = head,*cur;
        //统计节点个数,找到尾节点串成一个环
        while(pre->next != NULL){
            count++;
            pre = pre->next;
        }
        //串成一个环
        pre->next = head;
        //k可能大于链表长度
        k = k % count;
        int index = 1;
        pre = cur = head;
        //右移k位
        while(index <= (count - k)){
            pre = cur;
            cur = cur->next;
            index++;
        }
        //新的首尾节点
        pre->next = NULL;
        head = cur;
        return head;
    }
};
int main() {
    Solution solution;
    int A[] = {1,2,3,4,5};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 5;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.rotateRight(head->next,6);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}



【代码2】

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if (head == NULL || k == 0)
            return head;
        int len = 1;
        ListNode* p = head;
        while (p->next) { // 求长度
            len++;
            p = p->next;
        }
        k = len - k % len;
        p->next = head; // 首尾相连
        for(int step = 0; step < k; step++) {
            p = p->next; //接着往后跑
        }
        head = p->next; // 新的首节点
        p->next = NULL; // 断开环
        return head;
    }
};

【思路三】

/*-------------------------------------------------------------------
*   日期:2014-04-08
*   作者:SJF0115
*   题目: 61.Rotate List
*   来源:https://leetcode.com/problems/rotate-list/
*   结果:AC
*   来源:LeetCode
*   总结:
--------------------------------------------------------------------*/
#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(head == NULL || k <= 0){
            return head;
        }//if
        int size = 0;
        ListNode *p = head,*right = head;
        // 统计节点个数
        while(p){
            ++size;
            p = p->next;
        }//while
        // 找到右移的节点
        k = size - k % size;
        // 未右移
        if(k == size){
            return head;
        }//if
        ListNode *pre = nullptr;
        for(int i = 0;i < k;++i){
            pre = right;
            right = right->next;
        }//for
        pre->next = nullptr;
        p = right;
        // 到最后一个节点
        while(p->next){
            p = p->next;
        }//while
        p->next = head;
        return right;
    }
};
int main() {
    Solution solution;
    int A[] = {1,2,3,4,5};
    ListNode *head = new ListNode(-1);
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 5;i++){
        node = new ListNode(A[i]);
        pre->next = node;
        pre = node;
    }//for

    head = solution.rotateRight(head->next,2);
    while(head){
        cout<<head->val<<" ";
        head = head->next;
    }//while
    cout<<endl;
    return 0;
}

运行时间:





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics