Coding 时,要注意循环变量的意义。比如说这段代码中循环变量 i ,记录了 aheadNode 所处的位置;初始值是 1, 表示aheadNode 处在链表的第一个节点上。 // LastKElement.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> struct Node { Node(int n, Node * nextNode): i(n), m_next(nextNode) {} int i; Node * m_next; }; Node* recipNthNode(Node * header, unsigned int n) { if(n == 0 || header == NULL) return NULL; Node * aheadNode = header, * laterNode = header; unsigned int i = 1; while(aheadNode->m_next!= NULL) { i++; aheadNode = aheadNode->m_next; if( i > n) laterNode = laterNode->m_next; } if(i < n) return NULL; return laterNode; } int _tmain(int argc, _TCHAR* argv[]) { Node node3(3, NULL); Node node2(2, &node3); Node node1(1, &node2); Node * node = recipNthNode(&node1, 3); if(node) std::cout << node->i; return 0; }