<p>Given the <code>head</code> of a linked&nbsp;list, rotate the list to the right by <code>k</code> places.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg" style="width: 450px; height: 191px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5], k = 2
<strong>Output:</strong> [4,5,1,2,3]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg" style="width: 305px; height: 350px;" />
<pre>
<strong>Input:</strong> head = [0,1,2], k = 4
<strong>Output:</strong> [2,0,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[0, 500]</code>.</li>
	<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
	<li><code>0 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:

    /*int listSize(ListNode* head)
    {
        int l = 0;
        while (head)
        {
            head = head->next;
            ++l;
        }
        return l;
    }

    ListNode* rotateRightUtil(ListNode* head, const int& k, const int& l) {
        ListNode* old_head = head, * prev = nullptr;
        int j = l-k;
        while (j--) {
            prev = head;
            head = head->next;
        }
        prev->next = nullptr;
        prev = head;
        j = k;
        while (--j)
            prev = prev->next;

        prev->next = old_head;
        return head;
    }*/

    ListNode* rotateRight(ListNode* head, int k) {
        if (!head || !head->next)
            return head;

        ListNode* temp = head;
        int c = 1;
        while (head->next)
        {
            head = head->next;
            ++c;
        }

        if (k%c == 0)
            return temp;

        head->next = temp;

        k = k%c;
        int e = c - k;
        while (--e)
            temp = temp->next;

        head = temp->next;
        temp->next = nullptr;

        return head;


        
        
        /*int l = listSize(head);
        if (k%l == 0) return head;

        if (k > l)
            return rotateRightUtil(head, k%l, l);
        else
            return rotateRightUtil(head, k, l);
            */
        
    }
};

static const int __ = [](){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 0;
}();

int init = [] {
    ofstream out("user.out");
    cout.rdbuf(out.rdbuf());
    for (string str, k_str; getline(cin, str) && getline(cin, k_str); cout << '\n') {
        const int n = ranges::count(str, ',') + 1;
        int k = stoi(k_str) % n;
        if (n == 1 || k == 0) {
            cout << str;
            continue;
        }
        auto it = str.end() - 1;
        for (int commas = 0; it != str.begin() && commas != k; --it) if (*it == ',') ++commas;
        int dist = distance(str.begin(), it) + 2;
        string rotate = str.substr(dist, str.size() - 1 - dist);
        str.erase(it + 1, str.end() - 1);
        str.erase(0, 1);
        cout << '[' << rotate << ',' << str;
    }
    exit(0);
    return 0;
}();
7800