[LeetCode]61.Rotate List

160 篇文章 28 订阅
【题目】

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.

【题意】

给定一个链表,向右旋转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;
}

运行时间:





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值