Share me 4 ms C solution


  • 0

    more codes see: https://github.com/lightmen/leetcode.git

    struct ListNode* rotateRight(struct ListNode* head, int k) {

    struct ListNode root;
    struct ListNode *end;
    int size = 0;
    
    if(!head)
        return NULL;
    
    root.next = head;
    end = &root;
    
    while(head){
        end = head;
        head = head->next;
        size++;
    }
    
    k %= size;
    head = root.next;
    while(k < size-1){
        head = head->next;
        k++;
    }
    end->next = root.next;
    root.next = head->next;
    head->next = NULL;
    
    return root.next;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.