PAT 1097. Deduplication on a Linked List (25)

Source: Internet
Author: User

1097. Deduplication on a Linked List (25)

Given a singly linked list L with integer keys, you is supposed to remove the nodes with duplicated absolute values of th e keys. That's, for each value K, only the first node of which the value or absolute value of its key equals K would be kept. At the mean time, all the removed nodes must is 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 the one test case. The first line contains the address of the first node, and a positive N (<=) which are the total numb Er 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 a integer of which absolute value is no more than 104 , and next is the position of the next node.

Output Specification:

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

Sample Input:
00100 599999-7 8765423854-15 0000087654 15-100000-15 9999900100 21 23854
Sample Output:
00100 21 2385423854-15 9999999999-7-100000-15 8765487654 15-1

1#include <iostream>2#include <Set>3#include <queue>4 5 using namespacestd;6 7 structNode8 {9     intvalue;Ten     intNext; One }; A  -Node nodes[1000000]; -  the intMain () - { -queue<int>Res, DUP; -     Set<int>is_in; +  -     intstart, nodenum; +CIN >> Start >>Nodenum; A      for(inti =0; i < Nodenum; i++) at     { -         intaddress; -CIN >>address; -CIN >> Nodes[address].value >>Nodes[address].next; -     } -      while(Start! =-1) in     { -         if(Is_in.find (ABS (Nodes[start].value))!=is_in.end ())//Duplicate to Dup.push (start); +         Else -         { the Res.push (start); * Is_in.insert (ABS (Nodes[start].value)); $         }Panax NotoginsengStart =Nodes[start].next; -     } the     BOOLIsstart =true; +      while(!res.empty ()) A     { the         intAddress =Res.front (); + Res.pop (); -         if(!Isstart) $printf"%.05d\n", address); $printf"%.05d%d", address, nodes[address].value); -Isstart =false; -     } the     if(!Isstart) -cout <<"-1"<<Endl;Wuyi  theIsstart =true; -      while(!dup.empty ()) Wu     { -         intAddress =Dup.front (); About Dup.pop (); $         if(!Isstart) -printf"%.05d\n", address); -printf"%.05d%d", address, nodes[address].value); -Isstart =false; A     } +     if(!Isstart) thecout <<"-1"<<Endl; -}

PAT 1097. Deduplication on a Linked List (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.