力扣 题目19-- 删除链表的倒数第 N 个结点 一题多解

题目


题解


 

方法1:暴力解法1

先遍历一遍链表 然后把链表里面的ListNode* 放入容器中(比如 Map,vector) 然后用容器当作数组去操作

方法2:暴力解法2

遍历一遍记录长度然后再次遍历 当遍历到第 L-n+1Ln+1 个节点时,它就是我们需要删除的节点,然后删除即可。

方法3:暴力解法3 (源自力扣题目评论区 这位老哥的解法太有意思了)

递归到最后一个数时就会 开始执行下面的语句 cur++ 然后一层一层返回执行这个+法 当cur==n 找到对应节点 并且能够直接返回->next即可

方法4:巧解

1.在head前面创建一个节点second

2.创建节点first和head相等

3.拉开first和second的距离 这个距离是n

4.然后first和second 同时+1这样 当first到达最后一个时 second正好是倒数第n个然后进行删除操作

 

图来自链接

代码


 

代码1 对应暴力解法1

 1 #include<iostream>
 2 #include <vector>
 3 using namespace std;
 4 /**
 5  * Definition for singly-linked list. */
 6  struct ListNode {
 7      int val;
 8      ListNode *next;
 9      ListNode() : val(0), next(nullptr) {}
10      ListNode(int x) : val(x), next(nullptr) {}
11      ListNode(int x, ListNode *next) : val(x), next(next) {}
12  };
13 
14 class Solution {
15 public:
16     ListNode* removeNthFromEnd(ListNode* head, int n) {
17         struct ListNode* ergodic= head;
18         vector<ListNode*> v1;
19         for (int i = 0; ergodic != NULL;i++) {
20             v1.push_back(ergodic);
21             ergodic = ergodic->next;
22         }
23         ergodic = head;
24         if (n == v1.size()) {
25             head = v1[0]->next;
26         }
27         else if (n== 1) {
28             v1[v1.size() - 2]->next = NULL;
29         }
30         else if(n< v1.size()&& n > 1)
31         {
32             v1[v1.size()  - n-1]->next = v1[v1.size()  - n]->next;
33         }
34         return head;
35     }
36 };
37 
38 int main() {
39     Solution sol;
40     struct ListNode head(1);
41     struct ListNode *ergodic= &head;
42     for (int i = 2; i < 3; i++) {
43         struct ListNode *p;
44         p = (struct ListNode*)malloc(sizeof(struct ListNode*));
45         p->val = i;
46         ergodic->next = p;
47         ergodic = ergodic->next;
48     }
49     ergodic->next = NULL;
50     struct ListNode *ergodic2 =sol.removeNthFromEnd(&head,1);
51     for (int i = 0; ergodic2 != NULL; i++) {
52         cout << ergodic2->val << endl;
53         ergodic2 = ergodic2->next;
54     }
55 
56 }
View Code

代码2 巧解

 1 #include<iostream>
 2 #include <vector>
 3 using namespace std;
 4 /**
 5  * Definition for singly-linked list. */
 6  struct ListNode {
 7      int val;
 8      ListNode *next;
 9      ListNode() : val(0), next(nullptr) {}
10      ListNode(int x) : val(x), next(nullptr) {}
11      ListNode(int x, ListNode *next) : val(x), next(next) {}
12  };
13 
14 class Solution {
15 public:
16     ListNode* removeNthFromEnd(ListNode* head, int n) {
17         ListNode* dummy = new ListNode(0, head);
18         ListNode* first = head;
19         ListNode* second= dummy;
20         for (int i = 0; i < n; ++i) {
21             first = first->next;
22         }
23         while (first) {
24             first = first->next;
25             second = second->next;
26         }
27         second->next = second->next->next;
28         ListNode* ans = dummy->next;
29         delete dummy;
30         return ans;
31     }
32 };
33 
34 int main() {
35     Solution sol;
36     struct ListNode head(1);
37     struct ListNode *ergodic= &head;
38     for (int i = 2; i < 6; i++) {
39         struct ListNode *p;
40         p = (struct ListNode*)malloc(sizeof(struct ListNode*));
41         p->val = i;
42         ergodic->next = p;
43         ergodic = ergodic->next;
44     }
45     ergodic->next = NULL;
46     struct ListNode *ergodic2 =sol.removeNthFromEnd(&head,2);
47     for (int i = 0; ergodic2 != NULL; i++) {
48         cout << ergodic2->val << endl;
49         ergodic2 = ergodic2->next;
50     }
51 
52 }
View Code
posted @ 2022-04-06 11:19  无聊的阿库娅  阅读(24)  评论(0编辑  收藏  举报