solution.md 4.2 KB
Newer Older
1
# 删除链表的倒数第 N 个结点
F
fix bug  
feilong 已提交
2

F
fix bug  
feilong 已提交
3
<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p><p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], n = 2<strong><br />输出:</strong>[1,2,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [1], n = 1<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2], n = 1<strong><br />输出:</strong>[1]</pre><p><strong>提示:</strong></p><ul>	<li>链表中结点的数目为 <code>sz</code></li>	<li><code>1 <= sz <= 30</code></li>	<li><code>0 <= Node.val <= 100</code></li>	<li><code>1 <= n <= sz</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 21 22
#include <bits/stdc++.h>
using namespace std;

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

24
### after
F
fix bug  
feilong 已提交
25

每日一练社区's avatar
每日一练社区 已提交
26
```c
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
int main()
{
    Solution sol;
    int n = 2;
    ListNode *L1 = new ListNode();
    ListNode *l11 = new ListNode(1);
    ListNode *l12 = new ListNode(2);
    ListNode *l13 = new ListNode(3);
    ListNode *l14 = new ListNode(4);
    ListNode *l15 = new ListNode(5);

    L1->next = l11;
    l11->next = l12;
    l12->next = l13;
    l13->next = l14;
    l14->next = l15;

    ListNode *res = new ListNode();

    res = sol.removeNthFromEnd(L1, n);

    ListNode *p = res->next;

    while (p != NULL)
    {
        cout << p->val << " ";
        p = p->next;
    }

    return 0;
}
```

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

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

class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {

        if (head->next == NULL)
        {
            delete head;
            return NULL;
        }

        ListNode *fast = head;
        ListNode *slow = head;
        for (int i = 0; i < n; ++i)
            fast = fast->next;

        if (fast == NULL)
        {
            ListNode *temp = head->next;
            head->val = temp->val;
            head->next = temp->next;
            return head;
        }

        while (fast->next != NULL)
        {
            fast = fast->next;
            slow = slow->next;
        }
        slow = slow->next;
        return head;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
101

102
### A
F
fix bug  
feilong 已提交
103

每日一练社区's avatar
每日一练社区 已提交
104
```c
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
class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {

        auto dummy = new ListNode(0);
        dummy->next = head;

        auto lst = head;
        int len = 0;

        while (lst != NULL)
        {
            ++len;
            lst = lst->next;
        }

        int target = len + 1 - n;

        lst = dummy;
        while (target > 1)
        {
            lst = lst->next;
            --target;
        }
        lst->next = lst->next->next;

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

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

每日一练社区's avatar
每日一练社区 已提交
140
```c
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
class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        ListNode *h = new ListNode(0);
        h->next = head;
        ListNode *p = h;
        ListNode *k = h;
        if (head->next == NULL)
            return {};
        for (int i = 0; i <= n; i++)
        {
            k = k->next;
        }
        while (k)
        {
            p = p->next;
            k = k->next;
        }
        p->next = p->next->next;
        return h->next;
    }
};
```

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

每日一练社区's avatar
每日一练社区 已提交
169
```c
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        int count = 0;
        ListNode *p = head;
        while (p)
        {
            count++;
            p = p->next;
        }
        p = head;
        if (count == 1)
            return {};
        else if (count - n == 0)
            return head->next;
        else
        {
            for (int i = 1; i < count - n; i++)
            {
                p = p->next;
            }
            p->next = p->next->next;
        }
        return head;
    }
};
```