题解 | 牛牛的单向链表

牛牛的单向链表

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

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

// write your code here......
struct Node
{
    int data;
    struct Node* Next;
};

int main() {

    int n;
    scanf("%d",&n);

    int* arr=(int*)malloc(n*sizeof(int));

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

    // write your code here......
    struct Node* head=NULL;
    struct Node* tail=NULL;

    for(int i=0;i<n;i++)
    {
        struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
        newNode->data=arr[i];
        newNode->Next=NULL;
        if(head==NULL)
        {
            head=newNode;
            tail=newNode;
        }
        else {
        tail->Next=newNode;
        tail=newNode;
        }

    }

    struct Node* current=head;
    while(current!=NULL)
    {
        printf("%d ",current->data);
        current=current->Next;
    }

    

    free(arr);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务