题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
Node* next;
}Node;
Node* head;
void insert(int x) {
Node* temp = new Node();
temp->data = x;
temp->next = NULL;
head = temp;
Node* temp2 = head;
while (temp2->next!= NULL)
{
temp2= temp2->next;
}
temp2->next = temp;
temp->next=NULL;
}
void print() {
Node* temp = head;
while (temp!= NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
int main()
{
int n;
head = NULL;
scanf("%d", &n);
int i,x;
for (i = 0; i < n; i++)
{
scanf("%d", &x);
insert(x);
print();
}
return 0;
}

查看25道真题和解析