standard c++ code


  • 0
    B
    class Solution {
    public:
        ListNode* rotateRight(ListNode* head, int k) {
            if(!head) return head;
            ListNode* cur = head;
            int ctr = 1;
            while(cur->next) {
                cur = cur->next;
                ctr++;
            }
            cur->next = head;
            cur = head;
            int i = 0;
            while(i<(ctr-(k+1)%ctr)) {
                cur = cur->next;
                i++;
            }
            ListNode* ret = cur->next;
            cur->next = nullptr;
            return ret;
        }
    };
    

Log in to reply
 

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