<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>

 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;
}();
class Solution {
public:
    // First Go Through The Linked List To Find The Total Number Of Length.
    // Calculate The Actual Number Of Rotations Needed k%length.
    // Go Through The List At Element k%length and only swap from there.
    ListNode* rotateRight(ListNode* head, int k) {
        // Edge Cases
        if (head == nullptr || head->next == nullptr||k==0) {
            return head;
        }
        // Finding Length
        int length = 1;
        ListNode* current = head;
        ListNode* tail = head;
        while (tail->next != nullptr) {
            tail = tail->next;
            length++;
        }
        k = k % length;// Holds The Actual No. Of Rotations Needed.
        if (k == 0) {
            return head;
        }
        int rotation_index = length - k;
        int i = 1;
        // Setting Up The Current Pointer In Right Place
        while (i < rotation_index) {
            current = current->next;
            i++;
        }
        ListNode* saved_node = current->next;
        current->next = nullptr;
        tail->next = head;
        head = saved_node;
        return head;
    }
};
7600