题解 | #链表中倒数第k个结点#

链表中倒数第k个结点

https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
*
 * @param pListHead ListNode类 
 * @param k int整型 
 * @return ListNode类
 */
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) 
{
    // write code here
    if(pListHead==NULL)
    {
        return NULL;
    }
    struct ListNode* cur = pListHead;
    int i = 0;
    while(cur!=NULL)
    {
        i++;
        cur = cur->next;
    }
    cur=pListHead;
    if(i==1)
    {
        return cur;
    }
    if(k > i)
    {
        return NULL;
    }
    int j =0;
    for(j = 0;j<i-k;j++)
    {
        cur = cur->next;
    }
    return cur;
}

全部评论

相关推荐

陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
07-11 10:56
门头沟学院 Java
码客明:大胆的说自己能实习6个月就行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务