题解 | #牛牛的单向链表#

牛牛的单向链表

https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int data;
    struct Node* next;
} Node;

Node* creactNode(int data){
    Node* newNode=(Node*)malloc(sizeof(Node));
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
}

int main() {
    int n;
    scanf("%d",&n);
    int array[n];
    for (int i=0; i<n; i++) {
    scanf("%d",&array[i]);
    }

    Node* head=(Node*)malloc(sizeof(Node));
    head->data=array[0];
    head->next=NULL;

    Node* temp=head;
    for (int i=1; i<n; i++) {
    temp->next=creactNode(array[i]);
    temp=temp->next;
    }

    while (head) {
    printf("%d ",head->data);
    head=head->next;
    }

    return 0;
}

全部评论

相关推荐

你见过凌晨四点的牛客吗_BY_KobeBryant:明年再投都一样😂😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务