题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h> typedef struct LNode{ int data; struct LNode* next; }LNode,*LinkList; void Insert(LinkList L,int X){ //插入新节点的插入,头插法 LNode* S =(LNode*)malloc(sizeof(LNode)); S->next = L->next; S->data = X; L->next = S; } int main() { LinkList L; L=(LNode*)malloc(sizeof(LNode));//初始化一个带头节点的单链表 L->next=NULL; int n; scanf("%d",&n); int a[n]; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=n-1;i>=0;i--){ //由于写的是头插法,所以从数组的最后一个开始插入单链表 Insert(L,a[i]); } LNode* p=L->next;//因为是带头结点的单链表,头结点不存数据,从头结点的下一个节点开始遍历所存数据 while(p!=NULL){ printf("%d ",p->data); p=p->next; } return 0; }