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)


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