PAT 1097 Deduplication on a Linked List(25分)

原题链接:PAT 1097 Deduplication on a Linked List(25分)

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤10 5 ) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 10 4 , and Next is the position of the next node.

Output Specification:
For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

Sample Output:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

题目大意:

给了一个链表,链表中的元素有正有负。
现在要求我们将该链表按照节点值的绝对值进行去重,最终得到两个链表(一个包含去重后的所有节点,一个包含被去除的所有节点),并输出这两个链表。

方法一:静态链表

思路:

用静态链表存储下每一个节点,包含他的地址、值以及next。
遍历一遍链表,根据绝对值是否在set中出现过,分别将地址添加到两个vector中,最终按要求输出即可

C++ 代码:

#include<iostream>
#include<vector> 
#include<set>
#include<cmath>
using namespace std;

const int maxn = 1e5 + 10;

struct node{
	int val;
	int next;
}list[maxn];

int start, n;
set<int> s;

int main(){
	scanf("%d %d", &start, &n);
	while(n--){
		int addr, val, next;
		scanf("%d %d %d", &addr, &val, &next);
		list[addr].val = val;
		list[addr].next = next;
	}
	
	vector<int> a, b; // 存储去重后链表以及被去重链表 
	
	// 遍历链表 将节点地址按情况分别插入a和b 
	int i = start;
	while(i != -1){
		// 如果已经出现过 
		if(s.count(abs(list[i].val))){
			b.push_back(i);
		}
		else{
			s.insert(abs(list[i].val));
			a.push_back(i); 
		}
		
		i = list[i].next;
	}

	// 输出去重链表	
 	for(int i = 0; i < a.size(); i++ ){
 		printf("%05d %d ", a[i], list[a[i]].val); 
		if(i == a.size() - 1)	// 最后一个补上-1 
			puts("-1");
        else 
			printf("%05d\n", a[i + 1]);
	}
 		
 	// 输出被去重链表 
 	for(int i = 0; i < b.size(); i++ ){
 		printf("%05d %d ", b[i], list[b[i]].val);
 		if(i == b.size() - 1)
 			puts("-1");
		else
			printf("%05d\n", b[i + 1]); 
	 }
 		
	
	return 0;
} 

复杂度分析:

  • 时间复杂度:O(n),遍历了一遍链表
  • 空间复杂度:O(n),两个vector,以及一个set,长度均不会超过n
  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值