码迷,mamicode.com
首页 > 其他好文 > 详细

61.Rotate List

时间:2019-04-09 10:49:52      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:for   tno   new   turn   code   return   head   node   solution   

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if (!head) return NULL;
        int n = 1;
        ListNode *cur = head;
        while (cur->next) {
            ++n;
            cur = cur->next;
        }
        cur->next = head;
        int m = n - k % n;
        for (int i = 0; i < m; ++i) {
            cur = cur->next;
        }
        ListNode *newhead = cur->next;
        cur->next = NULL;
        return newhead;
    }
};

61.Rotate List

标签:for   tno   new   turn   code   return   head   node   solution   

原文地址:https://www.cnblogs.com/smallredness/p/10675421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!