<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Remove Nth Node From End of List ### Source - lintcode: [(174) Remove Nth Node From End of List](http://www.lintcode.com/en/problem/remove-nth-node-from-end-of-list/) ~~~ Given a linked list, remove the nth node from the end of list and return its head. Note The minimum number of nodes in list is n. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5->null. Challenge O(n) time ~~~ ### 題解 簡單題,使用快慢指針解決此題,需要注意最后刪除的是否為頭節點。讓快指針先走`n`步,直至快指針走到終點,找到需要刪除節點之前的一個節點,改變`node->next`域即可。 ### C++ ~~~ /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @param n: An integer. * @return: The head of linked list. */ ListNode *removeNthFromEnd(ListNode *head, int n) { if (NULL == head || n < 0) { return NULL; } ListNode *preN = head; ListNode *tail = head; // slow fast pointer int index = 0; while (index < n) { if (NULL == tail) { return NULL; } tail = tail->next; ++index; } if (NULL == tail) { return head->next; } while (tail->next) { tail = tail->next; preN = preN->next; } preN->next = preN->next->next; return head; } }; ~~~ 以上代碼單獨判斷了是否需要刪除頭節點的情況,在遇到頭節點不確定的情況下,引入`dummy`節點將會使代碼更加優雅,改進的代碼如下。 ### C++ dummy node ~~~ /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @param n: An integer. * @return: The head of linked list. */ ListNode *removeNthFromEnd(ListNode *head, int n) { if (NULL == head || n < 1) { return NULL; } ListNode *dummy = new ListNode(0); dummy->next = head; ListNode *preDel = dummy; for (int i = 0; i != n; ++i) { if (NULL == head) { return NULL; } head = head->next; } while (head) { head = head->next; preDel = preDel->next; } preDel->next = preDel->next->next; return dummy->next; } }; ~~~ ### 源碼分析 引入`dummy`節點后畫個圖分析下就能確定`head`和`preDel`的轉移關系了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看