题解 | #牛牛的链表添加节点#

牛牛的链表添加节点

https://www.nowcoder.com/practice/e33b79c2e15a41f9b541e73cd256124a

#include <stdio.h>
#include<malloc.h>

typedef struct node {
    int data;
    struct node* next;
} node;
int main() {
    int n, i,s;
    scanf("%d %d", &n, &i);
    s=i;
    node* head = malloc(sizeof(node)), *t1 = head, *t2 = head;
    while (n--) {
        node* t = malloc(sizeof(node));
        scanf("%d", &t->data);
        t1->next = t;
        t1 = t;
    }
  //找到第i个节点
    while (i--) {
        t2 = t2->next;
    }
  //在该节点后面添加一个新节点
    node* t = malloc(sizeof(node));
    t->data = s;
    t->next = t2->next;
    t2->next = t;
    while (head->next) {
        printf("%d ", head->next->data);
        head = head->next;
    }
    return 0;
}

#C#
0基础学C 文章被收录于专栏

0基础学C,从算法开始

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务