leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0061.cpp (592B)


0 class Solution { 1 int len(ListNode *head) { 2 int count = 0; 3 while (head) { 4 count++; 5 head = head->next; 6 } 7 return count; 8 } 9 10 public: 11 ListNode *rotateRight(ListNode *head, int k) { 12 if (!head) return nullptr; 13 14 k %= len(head); 15 ListNode *p, *t; 16 t = p = head; 17 for (; p->next; p = p->next) { 18 if (k) 19 k--; 20 else 21 t = t->next; 22 } 23 p->next = head; 24 head = t->next; 25 t->next = nullptr; 26 return head; 27 } 28 };