int LinkedList::DoStuff()
{
Node *Current = next_;
while ( Current != NULL )
{
Current = Current->next_;
length_++;
}
// At the last iteration we have reached the end/tail/last node
return length_;
}
Run Code Online (Sandbox Code Playgroud)
除了最后一个节点之外没有其他节点.我怎样才能穿过前端的尾端?
除非你的链表是双重链接的,否则这很难做到.递归是一种方式,假设你没有那么大的列表,你将耗尽堆栈空间,像这样(伪代码):
DoStuffBackwards (currNode) {
if (currNode != NULL) {
DoStuffBackwards (currNode->next);
// Process currNode here.
}
}
DoStuffBackwards (firstNode);
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为你继续调用DoStuffBackwards()下一个节点,直到你耗尽列表然后,当你回滚递归堆栈时,你处理每个节点.