题解 | #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;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:48
点赞 评论 收藏
分享
评论
3
1
分享
牛客网
牛客企业服务