题解 | #CC7 牛牛的单向链表#
牛牛的单向链表
http://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h>
#include <stdlib.h>
struct LinkNode {
int value;
struct LinkNode *next;
};
int main() {
int n;
scanf("%d", &n);
struct LinkNode *head = (struct LinkNode *)malloc(sizeof(struct LinkNode));
struct LinkNode *tail = head;
for (int i = 0; i < n; i++) {
scanf("%d", &tail->value);
if (i < n-1) {
tail->next = (struct LinkNode *)malloc(sizeof(struct LinkNode));
tail = tail->next;
}
tail->next = NULL;
}
tail = head;
while (tail) {
printf("%d ", tail->value);
tail = tail->next;
}
while(head) {
tail = head->next;
free(head);
head = tail;
}
return 0;
}