offer22链表中倒数第k个节点
代码
#include<stdio.h>
#include<string.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* getKthFromEnd(struct ListNode* head, int k){
struct ListNode *p,*q;
p = head;
q = head;
if(k==0)
return NULL;
k = k-1;
while (k--&&q!=NULL)
{
q = q->next;
}
if(q==NULL)
return NULL;
while (q->next!=NULL)
{
p = p->next;
q = q->next;
}
return p;
}
还没有评论,来说两句吧...