题解 | #链表中倒数第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
    int number = 0;
    struct ListNode* temp =pListHead;
    while(temp)
    {
      ++number;
      temp = temp->next; 
    }
    struct ListNode* temp1 =pListHead;
    int t = number-k;
    if(t < 0)
    {
     return NULL;
    }

    while(t > 0)
    {
        temp1 = temp1->next; 
      t--;
    }
    return temp1;

}

全部评论

相关推荐

许愿ssp的咸鱼很不想泡池子:import python as pyhton
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务