前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >61. Rotate List(Linked List-Medium)

61. Rotate List(Linked List-Medium)

作者头像
Jack_Cui
发布2018-01-08 16:06:49
4700
发布2018-01-08 16:06:49
举报
文章被收录于专栏:Jack-CuiJack-Cui

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

For example:

Given

代码语言:javascript
复制
1->2->3->4->5->NULL and k = 2

return

代码语言:javascript
复制
4->5->1->2->3->NULL

题目: 给定一个链表,通过k(非负)节点将链表旋转到右侧。

解读: 一直感觉这个题目的表述有些问题。我是这样理解才做对题的:保留后k个节点,比如example给出的k=2,那么保留4->5,前面的3个节点1->2->3右移到后面,形成新链表4->5->1->2->3。除此之外,k值也有可能是大于链表长度的,需要取余,获得真实的位移。

Language : c

代码语言:javascript
复制
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k) {
    int index = 0;
    int listlength = 1;
    struct ListNode *newlist = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *tail = (struct ListNode *)malloc(sizeof(struct ListNode));
    if(head == NULL){
        return NULL;
    }
    tail = head;
    newlist = head;
    while(tail->next){              //计算链表长度
        tail = tail->next;
        listlength++;
    }
    k = k % listlength;             //k可能大于链表的长度
    if(k == 0){                     //k等于链表长度,不旋转
        return head;
    }
    k = listlength - k;             //移动前面listlength-k个节点到右侧,后k个节点不动
    tail->next = head;              //尾节点连接首节点
    for(index; index < k-1; index++){      //找都新链表头
        newlist = newlist->next;
    }
    head = newlist->next;           //新链表头
    newlist->next = NULL;           //链表结尾赋值NULL
    return head;                    //返回链表头结点
}

Language : cpp

代码语言:javascript
复制
/**
 * Definition for singly-linked list.
 * 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){
            return NULL;
        }
        ListNode *tail = head;
        int listlength = 1;
        while(tail->next){              //计算链表长度
            tail = tail->next;
            listlength++;
        }
        k = k % listlength;             //k可能大于链表的长度
        if(k == 0){                     //k等于链表长度,不旋转
            return head;
        }
        k = listlength - k;             //移动前面listlength-k个节点到右侧,后k个节点不动
        tail->next = head;              //尾节点连接首节点
        ListNode *newlist = head;
        for(int index = 0; index < k-1; index++){      //找都新链表头
            newlist = newlist->next;
        }
        head = newlist->next;           //新链表头
        newlist->next = NULL;           //链表结尾赋值NULL
        return head;                    //返回链表头结点
    }
};

Language:python

代码语言:javascript
复制
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not head:
            return None
        tail = head
        listlength = 1
        while tail.next:
            tail = tail.next
            listlength += 1
        k = k % listlength
        if k == 0:
            return head
        k = listlength - k
        tail.next = head
        newlist = head
        for each in range(k - 1):
            newlist = newlist.next
        head = newlist.next
        newlist.next = None
        return head

LeetCode题目汇总: https://github.com/Jack-Cherish/LeetCode

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档