题目描述
输入一个链表,输出该链表中倒数第k个结点。
示例1
输入
{1,2,3,4,5},1
返回值
{5}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pHead, int k) {
map<int,ListNode*>number;
int tip=0,ans;
while(pHead!=NULL)
{
tip++;number[tip]=pHead;
pHead=pHead->next;
}
return number[tip-k+1];
}
};