- 遍历确定长度,在遍历确定位置
struct ListNode* getKthFromEnd(struct ListNode* head, int k){
int i,cnt=0;
struct ListNode *p;
p = head;
while(p){
cnt++;
p=p->next;
}
p = head;
for(i=0;i<cnt-k;i++)
{
p=p->next;
}
return p;
}
- 双指针,配置间隔
struct ListNode* getKthFromEnd(struct ListNode* head, int k){
int i;
struct ListNode *fastp,*lowp;
lowp = head;
fastp = head;
for(i=1;i<=k;i++)
{
fastp = fastp->next;
}
while(fastp)
{
fastp = fastp->next;
lowp = lowp->next;
}
return lowp;
}