• <legend id='5a64L'><style id='5a64L'><dir id='5a64L'><q id='5a64L'></q></dir></style></legend>
  • <small id='5a64L'></small><noframes id='5a64L'>

      <tfoot id='5a64L'></tfoot>

        <i id='5a64L'><tr id='5a64L'><dt id='5a64L'><q id='5a64L'><span id='5a64L'><b id='5a64L'><form id='5a64L'><ins id='5a64L'></ins><ul id='5a64L'></ul><sub id='5a64L'></sub></form><legend id='5a64L'></legend><bdo id='5a64L'><pre id='5a64L'><center id='5a64L'></center></pre></bdo></b><th id='5a64L'></th></span></q></dt></tr></i><div id='5a64L'><tfoot id='5a64L'></tfoot><dl id='5a64L'><fieldset id='5a64L'></fieldset></dl></div>

          <bdo id='5a64L'></bdo><ul id='5a64L'></ul>

      1. 寻找“从末端开始的第N个节点"链表的

        时间:2023-10-18
      2. <tfoot id='KYCpl'></tfoot>
      3. <small id='KYCpl'></small><noframes id='KYCpl'>

            <i id='KYCpl'><tr id='KYCpl'><dt id='KYCpl'><q id='KYCpl'><span id='KYCpl'><b id='KYCpl'><form id='KYCpl'><ins id='KYCpl'></ins><ul id='KYCpl'></ul><sub id='KYCpl'></sub></form><legend id='KYCpl'></legend><bdo id='KYCpl'><pre id='KYCpl'><center id='KYCpl'></center></pre></bdo></b><th id='KYCpl'></th></span></q></dt></tr></i><div id='KYCpl'><tfoot id='KYCpl'></tfoot><dl id='KYCpl'><fieldset id='KYCpl'></fieldset></dl></div>

                <tbody id='KYCpl'></tbody>
              • <bdo id='KYCpl'></bdo><ul id='KYCpl'></ul>
                <legend id='KYCpl'><style id='KYCpl'><dir id='KYCpl'><q id='KYCpl'></q></dir></style></legend>
                  本文介绍了寻找“从末端开始的第N个节点"链表的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这似乎返回了正确答案,但我不确定这是否真的是解决问题的最佳方式.好像我访问了前 n 个节点太多次了.有什么建议?请注意,我必须使用单向链表来执行此操作.

                  This seems to be returning the correct answer, but I'm not sure if this is really the best way to go about things. It seems like I'm visiting the first n nodes too many times. Any suggestions? Note that I have to do this with a singly linked list.

                  Node *findNodeFromLast( Node *head, int n )
                  {
                      Node *currentNode;
                      Node *behindCurrent;
                      currentNode = head;
                      for( int i = 0; i < n; i++ ) {
                          if( currentNode->next ) {
                              currentNode = currentNode->next;
                          } else {
                              return NULL;
                          }
                      }
                  
                      behindCurrent = head;
                      while( currentNode->next ) {
                          currentNode = currentNode->next;
                          behindCurrent = behindCurrent->next;
                      }
                  
                      return behindCurrent;
                  }
                  

                  推荐答案

                  另一种无需两次访问节点的方法如下:

                  Another way to do it without visiting nodes twice is as follows:

                  创建一个大小为 n 的空数组,从索引 0 开始指向该数组的指针,并从链表的开头开始迭代.每次访问一个节点时,将其存储在数组的当前索引中并推进数组指针.当您填充数组时,环绕并覆盖您之前存储的元素.当您到达列表末尾时,指针将指向列表末尾的第 n 个元素.

                  Create an empty array of size n, a pointer into this array starting at index 0, and start iterating from the beginning of the linked list. Every time you visit a node store it in the current index of the array and advance the array pointer. When you fill the array, wrap around and overwrite the elements you stored before. When you reach the end of the list, the pointer will be pointing at the element n from the end of the list.

                  但这也只是一个 O(n) 算法.你目前正在做的很好.我看不出有什么令人信服的理由来改变它.

                  But this also is just an O(n) algorithm. What you are currently doing is fine. I see no compelling reason to change it.

                  这篇关于寻找“从末端开始的第N个节点"链表的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:vector 是链表的特例吗? 下一篇:在单向链表上使用插入排序

                  相关文章

                    <legend id='4S95W'><style id='4S95W'><dir id='4S95W'><q id='4S95W'></q></dir></style></legend>
                        <bdo id='4S95W'></bdo><ul id='4S95W'></ul>
                    1. <i id='4S95W'><tr id='4S95W'><dt id='4S95W'><q id='4S95W'><span id='4S95W'><b id='4S95W'><form id='4S95W'><ins id='4S95W'></ins><ul id='4S95W'></ul><sub id='4S95W'></sub></form><legend id='4S95W'></legend><bdo id='4S95W'><pre id='4S95W'><center id='4S95W'></center></pre></bdo></b><th id='4S95W'></th></span></q></dt></tr></i><div id='4S95W'><tfoot id='4S95W'></tfoot><dl id='4S95W'><fieldset id='4S95W'></fieldset></dl></div>

                      <tfoot id='4S95W'></tfoot>

                      <small id='4S95W'></small><noframes id='4S95W'>