题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include "stdio.h"
typedef struct Node{
int data;
struct Node* next;
}node;
node* cai(int b){
node* k=(node* )malloc(sizeof(node*));
k->data=b;
k->next=NULL;
return k;
}
int main(){
int n;
scanf("%d",&n);
node* head=NULL;
node* tail=NULL;
for(int i=0;i<n;i++){
int v;
scanf("%d",&v);
node* p=cai(v);
if(head==NULL){
head=p;
tail=p;
}else{
tail->next=p;
tail=p;
}
}
node* o=head;
while(o!=NULL){
printf("%d ",o->data);
o=o->next;
}
o=head;
while(o!=NULL){
node* c=o;
o=o->next;
free(c);
}
}
