jjzjj

behindCurrent

全部标签

c++ - 查找链表的 "Nth node from the end"

这似乎返回了正确的答案,但我不确定这是否真的是处理事情的最佳方式。好像我访问前n个节点的次数太多了。有什么建议么?请注意,我必须使用单链表来执行此操作。Node*findNodeFromLast(Node*head,intn){Node*currentNode;Node*behindCurrent;currentNode=head;for(inti=0;inext){currentNode=currentNode->next;}else{returnNULL;}}behindCurrent=head;while(currentNode->next){currentNode=curren

c++ - 查找链表的 "Nth node from the end"

这似乎返回了正确的答案,但我不确定这是否真的是处理事情的最佳方式。好像我访问前n个节点的次数太多了。有什么建议么?请注意,我必须使用单链表来执行此操作。Node*findNodeFromLast(Node*head,intn){Node*currentNode;Node*behindCurrent;currentNode=head;for(inti=0;inext){currentNode=currentNode->next;}else{returnNULL;}}behindCurrent=head;while(currentNode->next){currentNode=curren