class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead == NULL || pListHead->next == NULL)
return pListHead;
ListNode* cur = pListHead;
int count = 0;
while(cur){
count++;
cur = cur->next;
}
if(k > count)
{
return nullptr;
}
int tmp = count-k;
for(int i = 0; i < tmp; i++)
{
pListHead = pListHead->next;
}
return pListHead;
}
};
链表中倒数第K个节点
最新推荐文章于 2023-02-22 19:45:18 发布