其他分享
首页 > 其他分享> > 1097 Deduplication on a Linked List

1097 Deduplication on a Linked List

作者:互联网

题目

题意:给定原链表,原链表只保留绝对值第一次出现的元素,其余重复元素放到移出链表内

tip:链表删除操作 

#include<iostream>
#include<set>
#include<cmath>
using namespace std;
int main() {
	int s,n;
	cin>>s>>n;
	int list[100003],data[100003];
	for(int i=0; i<n; ++i) {
		int a,da,b;
		cin>>a>>da>>b;
		data[a]=da;
		list[a]=b;
	}
	int t=s,q=100002,p=s,e=100002;
	list[e]=-1;
	set<int> temp;
	temp.insert(abs(data[s]));
	s=list[s];
	while(s!=-1) {
		if(temp.find(abs(data[s]))==temp.end()) {//前面没出现的情况 
			temp.insert(abs(data[s]));
			list[p]=s;
			p=s;
		} else {//前面已出现的情况 
			list[q]=s;
			q=s;
		}
		s=list[s];
	}
	list[p]=-1;
	list[q]=-1;
	while(t!=-1) {//移除后的链表 
		printf("%05d %d ",t,data[t]);
		t=list[t];
		if(t!=-1)
			printf("%05d\n",t);
		else cout<<"-1\n";
	}
	e=list[e];
	while(e!=-1) {//已移除的链表 
		printf("%05d %d ",e,data[e]);
		e=list[e];
		if(e!=-1)
			printf("%05d\n",e);
		else cout<<"-1\n";
	}
	return 0;
}

 

江楚郎(已婚 发布了298 篇原创文章 · 获赞 15 · 访问量 1万+ 私信 关注

标签:1097,temp,int,List,list,链表,abs,data,Linked
来源: https://blog.csdn.net/qq_40991687/article/details/104093435