/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findKthToTail(ListNode* pListHead, int k) {
int cnt=0,len=0;
for (ListNode *i=pListHead;i;i=i->next) len++;
for (ListNode *i=pListHead;i;i=i->next){
if (++cnt==len-k+1) return i;
}
return NULL;
}
};
可以用先后指针,减少时间复杂度
懒得用.能过就行.
先后指针是啥
妙啊~