solution.md 3.5 KB
Newer Older
1
# 旋转链表
F
fix bug  
feilong 已提交
2

3
<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>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10
```c
11 12 13 14 15 16 17 18 19 20
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
```
每日一练社区's avatar
每日一练社区 已提交
21

22
### after
F
fix bug  
feilong 已提交
23

每日一练社区's avatar
每日一练社区 已提交
24
```c
25 26 27 28

```

## 答案
F
fix bug  
feilong 已提交
29

每日一练社区's avatar
每日一练社区 已提交
30
```c
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
54

55
### A
F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57
```c
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
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
F
fix bug  
feilong 已提交
90

每日一练社区's avatar
每日一练社区 已提交
91
```c
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
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
F
fix bug  
feilong 已提交
128

每日一练社区's avatar
每日一练社区 已提交
129
```c
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
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;
    }
};
```