solution.md 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
# 旋转链表
<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>以下错误的选项是?</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;
    }
};
```