您的位置:首页 > Web前端 > Node.js

LeetCode - Remove Nth Node From End of List

2013-12-06 21:50 260 查看
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->next to $cur->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:

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == nullptr){
// 1CE here, supposed to be nullptr, wrote 'null'
return nullptr;
}

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

ListNode *p1, *p2;
int i;

p1 = head;
while(true){
p2 = p1;
for(i = 0; i < n; ++i){
if(p2 == nullptr){
break;
}
p2 = p2->next;
}
if(i < n){
// abnormal case, invalid n
// possibly n is too large
return head;
}
if(p2 == nullptr){
break;
}else{
// 1TLE here, didn't move p1 forward
p1 = p1->next;
}
}
// p1 is the node to be removed
p2 = p1->next;
while(p2 != nullptr){
p1->val = p2->val;
p1 = p1->next;
p2 = p1->next;
}

p2 = head;
while(p2->next->next != nullptr){
p2 = p2->next;
}
// 1RE here, sentence mistakenly put in the while loop.. silly~
delete p2->next;
p2->next = nullptr;

return head;
}
};


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:

// 1CE, 2RE, 1AC, why such a hurry... could've 1ac'ed...
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *p1 = nullptr, *p2 = nullptr;
// 1CE here, missing declaration of $i
int i;

if(nullptr == head) {
return nullptr;
}

p1 = head;
for(i = 0; i < n + 1; ++i) {
if(p1 == nullptr) {
break;
}else {
p1 = p1->next;
}
}
// 1RE here, i == n, not i == n - 1
if(i == n) {
// n == length of the list, valid
p1 = head;
head = head->next;
delete p1;
// 1RE here, forgot to return head;
return head;
}else if(i < n) {
// n > length of the list, invalid
return head;
}else {
// n < length of the list, valid
p2 = head;
while(p1 != nullptr && p2 != nullptr) {
p1 = p1->next;
p2 = p2->next;
}
p1 = p2->next;
p2->next = p1->next;
delete p1;
return head;
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: