zoukankan      html  css  js  c++  java
  • LeetCode

    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:

     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 };
  • 相关阅读:
    【Kafka】《Kafka权威指南》——从Kafka读取数据
    【Kafka】《Kafka权威指南》——分区partition
    【Kafka】《Kafka权威指南》——写数据
    【Kafka】《Kafka权威指南》入门
    六大设计原则
    EXCEPTION_ACCESS_VIOLATION(0xc0000005)
    属性文件——Java&Spring
    Maven——向Maven本地仓库中手动添加依赖包(ps:ojdbc.jar)
    使用Python轻松批量压缩图片
    Nginx常用命令,解决你日常运维的烦恼
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3462166.html
Copyright © 2011-2022 走看看