//p1开始就遍历,当p1计数到第K个结点的时候,p2开始遍历,当p1到表尾时,p2为所求结点
Node findLastKNode(Node head, int k){
Node p1,p2;
p1 = head;
p2 = head;
int count = 0;
while (p1 != null) {
p1 = p1->next;
count++;
if(count == k){
p2 = p2->next;
}
}
return p2;
}
寻找链表倒数第K个结点
最新推荐文章于 2024-07-21 23:54:26 发布