思路:
鏈表的題目捏雌,要么內(nèi)存邏輯代替法姆坚、要么快慢指針囱晴、要么先后指針膏蚓,要么多指針,本題可以用先后指針(一個(gè)先出發(fā)畸写,一個(gè)后出發(fā))
代碼:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead==NULL)
return NULL;
ListNode *forward=pListHead;
ListNode *target=pListHead;
for(int i=0;i<k;++i){
if(forward==NULL)
return NULL;
forward=forward->next;
}
while(forward){
forward=forward->next;
target=target->next;
}
return target;
}
};