<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                **一. 題目描述** Given a linked list, remove the n th 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. **二. 題目分析** 給出一個鏈表,?`n`是指刪除**倒數**第`n`個節點。這里的提示`n`的值默認是合法的。不過其實對輸入的n進行異常判斷也只需要幾句語句。 使用兩個指針,即快/慢指針的概念,其中一個指針先走`n`步,然后慢指針走,等到快指針走到結尾時,那么慢指針走到了需要刪除的節點的**前一個位置**。 這道題主要難點是考慮邊界問題,以及特殊情況(要刪除的是頭節點),如輸入`1->2->3->4`,?`n=4`,那么需要刪除`1`,此時只需將頭指針`head = head->next`就可以了。 **三. 示例代碼** ~~~ #include <iostream> struct ListNode { int value; ListNode* next; ListNode(int x): value(x), next(NULL){}; }; class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { if (head == NULL) return NULL; ListNode *fast = head; ListNode *slow = head; ListNode *temp = head; for (int i = 0; i < n ; i++) { fast = fast->next; if (fast) continue; else break; } while (fast) { fast = fast->next; temp = slow; slow = slow->next; } if (slow == head) { head = head->next; return head; } temp->next = slow->next; delete slow; return head; } }; ~~~ 結果: ![](https://box.kancloud.cn/2016-01-05_568bb5ec718db.jpg) ![](https://box.kancloud.cn/2016-01-05_568bb5ed7faaa.jpg) ![](https://box.kancloud.cn/2016-01-05_568bb5ed9156c.jpg) **四. 小結** 實際編程中經常會遇到邊界問題,不小心的錯誤很容易造成程序奔潰,關于指針和鏈表的使用技巧還需要進一步的學習。
                  <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>

                              哎呀哎呀视频在线观看