随笔- 509  文章- 0  评论- 151  阅读- 22万 

Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

Solution1:

  The first solution is simple but clumsy, by starting from the head node, checking if the current node is the n'th node from the end.

  If the node is found, remove it by pointing parent>nexttocur->next, and free the $cur node.

  Time complexity is O((len - n) * n), where $len is the length of the linked list. On average the complexity is O(len ^ 2).

  Space complexity is O(1).

Accepted code:

复制代码
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *removeNthFromEnd(ListNode *head, int n) {
12         // IMPORTANT: Please reset any member data you declared, as
13         // the same Solution instance will be reused for each test case.
14         if(head == nullptr){
15             // 1CE here, supposed to be nullptr, wrote 'null'
16             return nullptr;
17         }
18         
19         if(head->next == nullptr){
20             delete head;
21             return nullptr;
22         }
23         
24         ListNode *p1, *p2;
25         int i;
26         
27         p1 = head;
28         while(true){
29             p2 = p1;
30             for(i = 0; i < n; ++i){
31                 if(p2 == nullptr){
32                     break;
33                 }
34                 p2 = p2->next;
35             }
36             if(i < n){
37                 // abnormal case, invalid n
38                 // possibly n is too large
39                 return head;
40             }
41             if(p2 == nullptr){
42                 break;
43             }else{
44                 // 1TLE here, didn't move p1 forward
45                 p1 = p1->next;
46             }
47         }
48         // p1 is the node to be removed
49         p2 = p1->next;
50         while(p2 != nullptr){
51             p1->val = p2->val;
52             p1 = p1->next;
53             p2 = p1->next;
54         }
55         
56         p2 = head;
57         while(p2->next->next != nullptr){
58             p2 = p2->next;
59         }
60         // 1RE here, sentence mistakenly put in the while loop.. silly~
61         delete p2->next;
62         p2->next = nullptr;
63         
64         return head;
65     }
66 };
复制代码

Solution2:

  The previous solution is foolish indeed, but I did write it... (X_x)

  So it's better to justify the AC with a better solution.

  My solution is to find the (n + 1)th node from the list end, and remove the node next to it, namely the n'th node.

  Since $n is guaranteed to be valid, there's just one special case to handle: n = len, which means the node to remove is the head node, without a parent node. Either we new a parent node to point to the head node, or handle it with extra code. If you know the cost of a new action, you won't do it unless have to. Just one pass will solve the problem.

  Time complexity is O(len), space complexity is O(1).

Accepted code:

 

复制代码
 1 // 1CE, 2RE, 1AC, why such a hurry... could've 1ac'ed...
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     ListNode *removeNthFromEnd(ListNode *head, int n) {
13         ListNode *p1 = nullptr, *p2 = nullptr;
14         // 1CE here, missing declaration of $i
15         int i;
16         
17         if(nullptr == head) {
18             return nullptr;
19         }
20         
21         p1 = head;
22         for(i = 0; i < n + 1; ++i) {
23             if(p1 == nullptr) {
24                 break;
25             }else {
26                 p1 = p1->next;
27             }
28         }
29         // 1RE here, i == n, not i == n - 1
30         if(i == n) {
31             // n == length of the list, valid
32             p1 = head;
33             head = head->next;
34             delete p1;
35             // 1RE here, forgot to return head;
36             return head;
37         }else if(i < n) {
38             // n > length of the list, invalid
39             return head;
40         }else {
41             // n < length of the list, valid
42             p2 = head;
43             while(p1 != nullptr && p2 != nullptr) {
44                 p1 = p1->next;
45                 p2 = p2->next;
46             }
47             p1 = p2->next;
48             p2->next = p1->next;
49             delete p1;
50             return head;
51         }
52     }
53 };
复制代码

 

 posted on   zhuli19901106  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示