• 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
    
    • 1

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Sample Output:

    00100 21 23854
    23854 -15 99999
    99999 -7 -1
    00000 -15 87654
    87654 15 -1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    题目大意:

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

    方法一:静态链表

    思路:

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

    C++ 代码:

    #include
    #include 
    #include
    #include
    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;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    复杂度分析:

    • 时间复杂度:O(n),遍历了一遍链表
    • 空间复杂度:O(n),两个vector,以及一个set,长度均不会超过n
  • 相关阅读:
    计算机毕业设计SSMjspm基于框架的影视分享平台【附源码数据库】
    UE4 钥匙开门
    花30天整理了11个超棒的Java开源项目,雀氏牛逼
    Linux笔记 - - vim的使用
    Java:Java有多流行,有哪些主要应用程序?
    linux系统安全及应用
    182.Hive(四):企业级调优:执行计划,fetch抓取,本地模式,表的优化,案例实操
    苹果手机(iPhone)系统升级到IOS16.1后,发现连接WiFi、热点总是经常自动(随机)断开
    python+django就业信息管理系统e71hf
    基于51单片机的环境温湿度光强监测系统proteus仿真原理图PCB
  • 原文地址:https://blog.csdn.net/cwtnice/article/details/138077618