题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
#include <stdio.h> #include <stdlib.h> struct ListNode { int m_nKey; struct ListNode *m_pNext; }; struct ListNode* create_node(void) { struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode *)); node->m_pNext=NULL; node->m_nKey=0; return node; } int main() { int a, b,i,j; while (scanf("%d", &a) != EOF) { struct ListNode* newNode=NULL,*end=NULL,*head=NULL; for(i=0;i<a;i++) { // if(i==a) // { // scanf("%d",&b); // break; // } struct ListNode* newNode=create_node(); scanf("%d",&newNode->m_nKey); if(head==NULL) head=newNode; else end->m_pNext=newNode; end=newNode; // printf("%d",newNode->m_nKey); } if(end!=NULL) end->m_pNext=NULL; scanf("%d",&b); j=a-b; while(j) { head=head->m_pNext; j--; } printf("%d\n",head->m_nKey); } return 0; }
注意单链表的创建