题解 | #输出单向链表中倒数第k个结点#

输出单向链表中倒数第k个结点

https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/*
 *注意题目中提到构建单向链表后需要忘记长度
 */
struct ListNode
{
    int m_nKey;
    struct ListNode* m_pNext;
};
int main() {
    int num=0,i=0,output=0;
    struct ListNode *tmp=NULL,*head=NULL,*rear=NULL;
    while (scanf("%d", &num) != EOF) {
        head=(struct ListNode *)malloc(sizeof(struct ListNode));
        head->m_nKey=0;head->m_pNext=NULL;
        rear=head;
        for(i=0;i<num;++i)
        {
            rear->m_pNext=(struct ListNode*)malloc(sizeof(struct ListNode));
            rear=rear->m_pNext;rear->m_pNext=NULL;
            scanf("%d", &rear->m_nKey);
        }
        scanf("%d",&output);
        tmp=head->m_pNext;num=0;
        while(tmp) 
        {
            num++;tmp=tmp->m_pNext;
        }
        if(output>num) 
        {
            printf("");
        }
        else {
		  tmp=head->m_pNext;
		  for(i=0;i<num-output;++i)
		  {
			  tmp=tmp->m_pNext;
		  }
		  printf("%d\n",tmp->m_nKey);
        }
    }
    return 0;
}

全部评论

相关推荐

鱼专:别投了,我看到有人点了第二个链接投递,还没退出界面,不合适的邮件就发过来了
点赞 评论 收藏
分享
03-17 23:54
黑龙江大学 Java
来个白菜也好啊qaq:可以的,大厂有的缺打手
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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