【leetcode】61. 旋转链表

 

struct ListNode* rotateRight(struct ListNode* head, int k){
    if(!head) return NULL;
    struct ListNode* arr[1000];
    int cnt=0, i, n;
    while(head){
        arr[cnt++]=head;
        head=head->next;
    }
    if(k%cnt == 0 || cnt==1)
        return arr[0];
    arr[cnt-1]->next=arr[0];
    arr[cnt-k%cnt-1]->next=NULL;
    return arr[cnt-k%cnt];
}

 

posted @ 2020-12-11 11:36  温暖了寂寞  阅读(74)  评论(0编辑  收藏  举报