A intuitive and clean solution accepted as best submission in C, well commented

  • 0
    //AC - 4ms;
    struct ListNode* rotateRight(struct ListNode* head, int k)
        if(!head) return NULL;
        int len = 0;
        struct ListNode* p = head;
            len++, p = p->next;
        k %= len; //after a circle of rotation, the chain will be the same;
        if(!k) return head;
        struct ListNode *cur=head, *kth=head;
        while(k) //move forward by k nodes;
            kth=kth->next, k--;
        while(kth->next) //there will be k nodes after cur - end of the loop;
            cur = cur->next;
            kth = kth->next;
        kth = cur->next; //the head of the right chain;
        cur->next = NULL;
        cur = kth; //the new head;
            kth = kth->next; //move to the end of the right chain;
        kth->next = head; //connect the left to the end of the right;
        return cur;

Log in to reply

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