<C/C++>从单向链表中删除指定值的节点

题目描述

输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针。

链表的值不能重复。

构造过程,例如

1 <- 2
3 <- 2
5 <- 1
4 <- 5
7 <- 2

最后的链表的顺序为

2 7 3 1 5 4

删除 结点 2

则结果为 7 3 1 5 4

链表长度不大于1000,每个节点的值不大于10000。
本题含有多组样例。

输入描述:

1 输入链表结点个数
2 输入头结点的值
3 按照格式插入各个结点
4 输入要删除的结点的值

输出描述:

输出删除结点后的序列,每个数后都要加空格

示例1
输入

5
2
3 2
4 3
5 2
1 4
3

输出

2 5 4 1

C++解法

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;

struct Node {
public:
    Node(int value) {
        val = value;
        next = NULL;
    }
    int val;
    struct Node *next;
};
vector<struct Node*> nodes;

struct Node* getNode(int value) {
    int length = nodes.size();
    struct Node *node = NULL;
    for (int i = 0; i < length; i++) {
        struct Node *cur = nodes.at(i);
        if (cur->val == value) {
            return cur;
        }
    }
    if (node == NULL) {
        node = new struct Node(value);
        nodes.push_back(node);
    }
    return node;
}

struct Node* deleteNode(int dst, struct Node *root) {
    struct Node *oldRoot = root;
    struct Node *previous = root;
    while (root != NULL) {
        if (root->val == dst) {
            if (root == oldRoot) {
                oldRoot = root->next;
                root->next = NULL;
            } else {
                previous->next = root->next;
                root->next = NULL;
            }
            return oldRoot;
        }
        previous = root;
        root = root->next;
    }
    return oldRoot;
}

void linkNode(int dest, int src) {
    struct Node *srcNode = getNode(src);
    struct Node *dstNode = getNode(dest);
    
    struct Node *oldNext = srcNode->next;
    srcNode->next = dstNode;
    dstNode->next = oldNext;
}

void parseLink(string input) {
    int size = input.length();
    char single;
    char buffer[20] = {0};
    char bufferIndex = 0;
    int nodes[2] = {0};
    int nodeIndex = 0;
    for (int i = 0; i <= size; i++) {
        if (i == size) {
            single = ' ';
        } else {
            single = input.at(i);
        }
        if (single == ' ') {
            nodes[nodeIndex++] = atoi(buffer);
            bufferIndex = 0;
            memset(buffer, 0, 20);
        } else {
            buffer[bufferIndex++] = single;
        }
    }
    linkNode(nodes[0], nodes[1]);
}

void printLinkedList(struct Node *root) {
    while (root != NULL) {
        cout << root->val << " ";
        root = root->next;
    }
    cout << endl;
}

void clearNodes() {
    for (int i = 0; i < nodes.size(); i++) {
        delete nodes.at(i);
    }
    nodes.clear();
}

int main()
{
    std::string str;
    vector<string> inputs;
    int length = 0;
    while (getline(std::cin, str)) {
        inputs.push_back(str); 
    }
    length = atoi(inputs.at(0).c_str());
    int rootValue = atoi(inputs.at(1).c_str());
    struct Node *root = getNode(rootValue);
    for (int i = 1; i < length; i++) {
        parseLink(inputs.at(1+i));
    }
    int deleteValue = atoi(inputs.at(length+1).c_str());
    root = deleteNode(deleteValue, root);
    printLinkedList(root);
    
    clearNodes();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值