# 旋转链表

<p>给你一个链表的头节点 <code>head</code> ，旋转链表，将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/rotate1.jpg" style="width: 600px; height: 254px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 2<strong><br />输出：</strong>[4,5,1,2,3]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/roate2.jpg" style="width: 472px; height: 542px;" /><pre><strong>输入：</strong>head = [0,1,2], k = 4<strong><br />输出：</strong>[2,0,1]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点的数目在范围 <code>[0, 500]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>0 <= k <= 2 * 10<sup>9</sup></code></li></ul>
<p>以下<font color="red">错误</font>的选项是？</p>

## aop

### before

```cpp
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
```
### after

```cpp

```

## 答案

```cpp
class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (!head || !head->next || k == 0)
            return head;
        int len = 1;
        ListNode *cur = head;
        while (cur->next && ++len)
            cur = cur->next;
        cur->next = head;
        k = (len - k) % len;
        while (k--)
            cur = cur->next;
        head = cur->next;
        cur->next = nullptr;
        return head;
    }
};
```
## 选项


### A

```cpp
class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (k == 0 || !head || !(head->next))
            return head;
        ListNode *temp = head;
        int len = 0;
        while (temp)
        {
            ++len;
            temp = temp->next;
        }
        k = k % len;
        temp = head;
        for (int i = len - 1; i > 0; --i)
            temp = temp->next;
        temp->next = head;
        temp = head;
        for (int j = len - k; j > 0; --j)
            temp = temp->next;
        head = temp;
        for (int m = len - 1; m > 0; --m)
            temp = temp->next;
        temp->next = nullptr;
        return head;
    }
};
```

### B

```cpp
class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (head == NULL)
            return head;
        ListNode *p = head;
        ListNode *q = head;
        int len = 1;

        while (p->next)
        {
            len++;
            p = p->next;
        }
        k %= len;
        if (k == 0)
            return head;

        p->next = head;

        int index = 1;
        while (index < (len - k))
        {
            index++;
            q = q->next;
        }
        ListNode *ret = q->next;
        q->next = NULL;
        return ret;
    }
};
```

### C

```cpp
class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (!head || !k || !head->next)
            return head;
        ListNode *temp = head;
        int n;
        for (n = 1; temp->next != NULL; n++)
        {
            temp = temp->next;
        }
        temp->next = head;
        ListNode *Node = head;
        for (int i = 0; i < n - k % n - 1; i++)
        {
            Node = Node->next;
        }
        ListNode *new_head = Node->next;
        Node->next = NULL;
        return new_head;
    }
};
```
