题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int value;
struct node* next;
}Node;
int main(){
int a;
scanf("%d",&a);
int i,x;
Node* head=NULL;
Node* p=NULL;
for(i=0;i<a;i++){
scanf("%d",&x);
if(head==NULL){
head=(Node*)malloc(sizeof(Node));
head->value=x;
head->next=NULL;
}
else {
p=head;
Node* q=NULL;
while(p->next){
p=p->next;
}
q=(Node*)malloc(sizeof(Node));
q->value=x;
p->next=q;
q->next=NULL;
}
}
for(p=head;p;p=p->next){
printf("%d",p->value);
printf(" ");
}
return 0;
}
#include<stdlib.h>
typedef struct node{
int value;
struct node* next;
}Node;
int main(){
int a;
scanf("%d",&a);
int i,x;
Node* head=NULL;
Node* p=NULL;
for(i=0;i<a;i++){
scanf("%d",&x);
if(head==NULL){
head=(Node*)malloc(sizeof(Node));
head->value=x;
head->next=NULL;
}
else {
p=head;
Node* q=NULL;
while(p->next){
p=p->next;
}
q=(Node*)malloc(sizeof(Node));
q->value=x;
p->next=q;
q->next=NULL;
}
}
for(p=head;p;p=p->next){
printf("%d",p->value);
printf(" ");
}
return 0;
}