1097 Deduplication on a Linked List

cpp 复制代码
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100010;
struct Node{
    int address, data, next, flag;
    Node(){
        flag = 0;
    }
}nodes[maxn];
int ma[10001];
int del(int pre, int cur){
    int next = nodes[cur].next;
    nodes[pre].next = next;
    return next;
}
int main() {
    int head,n;
    scanf("%d%d", &head, &n);
    int add;
    for(int i = 0; i < n; i++){
        scanf("%d", &add);
        scanf("%d%d", &nodes[add].data, &nodes[add].next);
        nodes[add].address = add;
    }
    ma[abs(nodes[head].data)] = 1;
    int pre=head,cur=nodes[head].next,d;
    vector<int> delNodes;
    while(cur != -1){
        int d = abs(nodes[cur].data);
        if(ma[d] == 1){
            delNodes.push_back(cur);
            cur = del(pre,cur);
        }else{
            ma[abs(nodes[cur].data)] = 1;
            pre = cur;
            cur = nodes[cur].next;
        }
    }
    int p;
    for(p = head; p != -1; p = nodes[p].next){
        if(nodes[p].next != -1){
            printf("%05d %d %05d\n", nodes[p].address, nodes[p].data, nodes[p].next);
        }else{
            printf("%05d %d -1\n", nodes[p].address, nodes[p].data);
        }
    }
    for(int i = 0; i < delNodes.size(); i++){
        if(i < delNodes.size()-1){
            printf("%05d %d %05d\n", nodes[delNodes[i]].address, nodes[delNodes[i]].data, nodes[delNodes[i+1]].address);
        }else{
            printf("%05d %d -1\n", nodes[delNodes[i]].address, nodes[delNodes[i]].data);
        }
    }
    return 0;
}
相关推荐
想做小南娘,发现自己是女生喵几秒前
【无标题】
数据结构·算法
Kx_Triumphs2 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎2 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
轻颂呀3 小时前
约瑟夫环问题
算法
凤凰院凶涛QAQ4 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
科技大视界5 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘
郝学胜-神的一滴5 小时前
算法实战:最小k个数——大顶堆的优雅解法
开发语言·数据结构·c++·python·程序人生·算法·排序算法
Irissgwe5 小时前
算法滑动窗口
数据结构·算法
怪兽学LLM5 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展