- 链表中倒数最后k个结点 -> 链表
- 前言
- 一、实现步骤
- 1.双指针法
- 结果
前言
一、实现步骤 1.双指针法
代码如下(示例):
class Solution {
public:
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
if (pHead == NULL)
return NULL;
ListNode* cur = pHead;
ListNode* fast = pHead;
while (k--) {
if (fast == NULL)
return NULL;
else
fast = fast->next;
}
while (fast != NULL) {
fast = fast->next;
cur = cur->next;
}
return cur;
}
};
结果



