Week 07 - Leetcode 61 - 70

class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return head;
int n = 1;
ListNode* tail = head, *cur = head, *res = head;
while(tail->next)
tail = tail->next, n++;
tail->next = head;
k = n - 1 - (k % n);
while(k--)
cur = cur->next;
res = cur->next;
cur->next = nullptr;
return res;
}
};

Last updated