LIUPENG BLOG
Liupeng
Jun 12, 2020
It takes 1 minutes to read this article.

旋转链表

Title

截屏2020-06-12 下午10.35.37

My Code

ListNode *Paly61::rotateRight(ListNode *head, int k) {
    if (head == NULL) return NULL;
    if (head->next == NULL) return head;
    auto number = 1;
    ListNode* templ = head;
    while(templ->next != NULL) {
        number = number + 1;
        templ = templ->next;
    }
    if (k != 0) k = k % number;
    for (int i=0; i<k; i++) {
        ListNode* temp = head;
        ListNode* beforTemp = head;
        while(temp->next != NULL) {
            beforTemp = temp;
            temp = temp->next;
        }
        beforTemp->next = NULL;
        temp->next = head;
        head = temp;
    }
    return head;
}

Result

截屏2020-06-12 下午10.36.27