题解 | 牛牛的单向链表

牛牛的单向链表

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

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

// write your code here......
typedef struct lnode{
    int data;
    struct lnode *next;
}lnode,*linklist;


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......
    //the end insert
    linklist L=NULL;
    L=(lnode*)malloc(sizeof(lnode));
    L->next=NULL;//Initial
    lnode*r=L;
    int i=0;
     while(r!=NULL&&i<n)
     {
       lnode*p=(lnode*)malloc(sizeof(lnode));
       p->data=arr[i];
       p->next=r->next;
       r->next=p;
       r=r->next;
       i++;
     }
     int j=0;lnode*p=L;
     while(j<n)
     {
        p=p->next;
         printf("%d ",p->data);
        j++;
     }
     
     
    




    free(arr);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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