c++ - 链表的中间

标签 c++

当我们不知道链表的大小并且必须仅使用一个循环和一个指针执行时,如何找到链表的中间点。

最佳答案

怎么样

LinkedList * llist = getLList(); // the linked list
Node * node = llist.head;

while ( node ) {
    node = node.next;
    if ( node ) {
        node  = node.next;
        llist.remove( llist.head );
    }
}
// now llist.head is (er, um... was) the middle node.  
// hope you didn't need the rest of the list.

关于c++ - 链表的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1746594/

相关文章:

c++ - 无法实现接口(interface)的 [[deprecated]] 方法

c++ - 任何从 c++filt demangled 输出中获取更多信息的方式

c++ - 如何处理覆盖某些函数名称的 WinAPI 宏?

C++:file.seekg() 似乎没有返回当前位置

c++ - 当小部件太大时的 Gtk 3

c++ - OpenGl SuperBible实现相机俯仰(向上和向下看)

c++ - QT - 检测按下了哪个按钮

c++ - native C++ 类和 WinRT 组件(ref 类)之间的自动映射?

c++ - 在没有限定的情况下从类内的枚举解析名称

c++ - sleep () 与 sleep_for()