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

牛牛的链表添加节点

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

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

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

void freeNode(struct Node* cur) {
    if (cur->next != NULL)
        freeNode(cur->next);
    free(cur);
}

int main() {
    int count, key, value;

    Node* head = (Node*)malloc(sizeof(Node));
    Node* current = head;

    int i = 0;

    scanf("%d %d", &count, &key);

    for (i = 0; i < count; i++) {
        scanf("%d", &value);
        current->data = value;
        current->next = (Node*)malloc(sizeof(Node));
        current = current->next;
    }

    current = head;


    for (int i = 0; i < key - 1; i++) {
        current = current->next;
    }

    Node* tmp = current->next;
    current->next = (Node*)malloc(sizeof(Node));
    current->next->data = key;
    current->next->next = tmp;

    current = head;

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

    return 0;
}

全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 Java
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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