0202 返回倒数第k个节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


int kthToLast(struct ListNode* head, int k){
    struct ListNode *fast=head,*slow=head;
    int i;
    for(i=0;i<k;i++)
        fast=fast->next;
    while(fast!=NULL)
    {
        fast=fast->next;
        slow=slow->next;
    }
    return slow->val;

}

发布了64 篇原创文章 · 获赞 4 · 访问量 4327

猜你喜欢

转载自blog.csdn.net/yuppie__1029/article/details/105710549