22.链表中倒数第k个节点

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


struct ListNode* getKthFromEnd(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)
    {
        slow=slow->next;
        fast=fast->next;
    }
    return slow;

}

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

猜你喜欢

转载自blog.csdn.net/yuppie__1029/article/details/105710331
今日推荐