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

牛牛的链表添加节点

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;
}

全部评论

相关推荐

预计下个星期就能开奖吧,哪位老哥来给个准信
华孝子爱信等:对接人上周说的是这周
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务