建立链表和删除指定节点

题目描述:https://www.nowcoder.com/practice/0ab593ca56b1476eb05b1ff848fd7fcc?tpId=107&tags=&title=&diffculty=0&judgeStatus=0&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking
简单描述:先输入n个整数,按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。输出删除后的单链表信息。

#include <stdio.h>
#include <stdlib.h>
struct Node{
    int data;
    Node *next;
};
int main(){
    int n,ele,m,len=0;
    scanf("%d",&n);
    Node *head = (Node *)malloc(sizeof(Node));
    Node *p,*r;
    p=head;p->next=NULL;
    for (int i = 0; i<n; i++){
        scanf("%d",&ele);
        r = (Node *)malloc(sizeof(Node));
        r->data = ele;
        p->next = r;
        p = r;
        len++;
    }
    r->next = NULL;
    scanf("%d",&m);
    p=head;r=p->next;
    while(r!=NULL){
        if (r->data==m) {
            p->next = r->next;
            free(r);
            len--;
        }else{
             p=p->next;
        }
        r=p->next;
    }
    printf("%d\n",len);
    p=head->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 11:27
明天又是董事长面,啥时候是个头啊
在太阳里长大的人:公司就仨人吧😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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