题解 | #牛牛的链表删除#

牛牛的链表删除

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

#include <iostream>
using namespace std;
struct Node
{
    int num;
    Node* next;
};

void bulid_link_list(Node* dummy_head, int n)
{
    Node* temp = new Node;
    temp =  dummy_head;
    for(int i=0;i < n;i++)
    {
        Node* cur = new Node;
        temp->next = cur;
        cin >> cur->num;
        temp = temp->next;
    }
    temp->next = NULL;
}


void print(Node* dummy_head)
{
    Node* temp = new Node;
    temp = dummy_head->next;
    while (temp != NULL)
    {
        cout << temp->num << " ";
        temp = temp->next;
    }
}


int main()
{
    int n;
    cin >> n;
    int val;
    cin >> val;
    Node* dummy_head_a = new Node;
    bulid_link_list(dummy_head_a, n);
    Node* cur = dummy_head_a;
    while(cur != NULL && cur->next != NULL)
    {
        if (cur->next->num == val)
        {
            cur->next = cur->next->next;
        }
        cur = cur->next;
    }
    print(dummy_head_a);
    
}

全部评论

相关推荐

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