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

链表中倒数最后k个结点

https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pHead ListNode类 
 * @param k int整型 
 * @return ListNode类
 */
struct ListNode* FindKthToTail(struct ListNode* pHead, int k )
 {
    // write code here
     int ret = k-1;
     int count = 0;
     int n = 0;
    struct ListNode*pcur = pHead;
    while(pcur)
    {
        ++count;
        pcur = pcur->next;
    }
    if(count<k)
    {
        return NULL;
    }
    pcur = pHead;
    count-=ret;
    while(pcur)
    {
        ++n;
        if(n==count)
        {
            return pcur;
        }
        pcur = pcur->next;
    }
    return NULL;
}

全部评论

相关推荐

08-21 16:35
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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