solution.md 4.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 反转链表 II
F
fix bug  
feilong 已提交
2

F
fix bug  
feilong 已提交
3
给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。<p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p><strong>提示:</strong></p><ul>	<li>链表中节点数目为 <code>n</code></li>	<li><code>1 <= n <= 500</code></li>	<li><code>-500 <= Node.val <= 500</code></li>	<li><code>1 <= left <= right <= n</code></li></ul><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10
```c
每日一练社区's avatar
每日一练社区 已提交
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

每日一练社区's avatar
每日一练社区 已提交
22
### after
F
fix bug  
feilong 已提交
23

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

```

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

每日一练社区's avatar
每日一练社区 已提交
30
```c
每日一练社区's avatar
每日一练社区 已提交
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 *reverseBetween(ListNode *head, int m, int n)
    {
        ListNode *dummy = new ListNode(-1), *pre = dummy;
        dummy->next = head;
        for (int i = 0; i < m; ++i)
            pre = pre->next;
        ListNode *cur = pre->next;
        for (int i = m; i < n - 1; ++i)
        {
            ListNode *t = cur->next;
            cur->next = t->next;
            t->next = pre->next;
            pre->next = t;
        }
        return dummy->next;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
54

每日一练社区's avatar
每日一练社区 已提交
55
### A
F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57
```c
每日一练社区's avatar
每日一练社区 已提交
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
class Solution
{
public:
    ListNode *reverseBetween(ListNode *head, int m, int n)
    {
        if (head == nullptr || head->next == nullptr || m == n)
            return head;
        ListNode *pre = nullptr, *cur = head;
        while (m > 1)
        {
            pre = cur;
            cur = cur->next;
            m--;
            n--;
        }
        ListNode *tail = cur, *con = pre;
        ListNode *third;
        while (n > 0)
        {
            third = cur->next;
            cur->next = pre;
            pre = cur;
            cur = third;
            n--;
        }
        if (con)
        {
            con->next = pre;
        }
        else
        {
            head = pre;
        }

        tail->next = cur;
        return head;
    }
};
```

### B
F
fix bug  
feilong 已提交
99

每日一练社区's avatar
每日一练社区 已提交
100
```c
每日一练社区's avatar
每日一练社区 已提交
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
class Solution
{
public:
    ListNode *reverseBetween(ListNode *head, int m, int n)
    {
        if (head == NULL || m == n)
            return head;
        ListNode *pm = head, *pn = head;
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        ListNode *pm0 = dummy;
        while (m-- > 1)
            pm = pm->next, pm0 = pm0->next;
        while (n-- > 1)
            pn = pn->next;

        ListNode *pPre = pm, *pCur = pPre->next, *pn2 = pn->next;

        while (pCur != pn2)
        {
            if (pCur)
            {
                ListNode *pNext = pCur->next;
                pCur->next = pPre;
                pPre = pCur;
                pCur = pNext;
            }
        }
        pm0->next = pn;
        pm->next = pn2;

        return dummy->next;
    }
};
```

### C
F
fix bug  
feilong 已提交
138

每日一练社区's avatar
每日一练社区 已提交
139
```c
每日一练社区's avatar
每日一练社区 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
class Solution
{
public:
    ListNode *reverseBetween(ListNode *head, int m, int n)
    {
        if ((head == NULL) || (head->next == NULL))
            return head;
        ListNode *first = new ListNode(0);
        first->next = head;
        int cnt = 1;
        for (int i = 0; i < m - 1; ++i)
        {
            first = first->next;
            cnt++;
        }
        ListNode *old_pre = first;
        ListNode *cur = first->next;
        ListNode *cur_next = cur->next;
        ListNode *new_pre = cur;
        ListNode *temp;
        /*反转*/
        while ((cur != NULL) && (cnt < n))
        {
            temp = cur_next->next;
            cur_next->next = cur;
            cur = cur_next;
            cur_next = temp;
            cnt++;
        }

        old_pre->next = cur;
        new_pre->next = cur_next;
        if (m == 1)
            return cur;
        else
            return head;
    }
};
```