L2-002 linked list weight removal (25 points)

Given a linked list L with integer key values, you need to delete the key value nodes with duplicate absolute values. That is, for each key value K, only the node with the first absolute value equal to K is retained. At the same time, all deleted nodes must be saved in another linked list. For example, given that l is 21 → - 15 → - 15 → - 7 → 15, you need to output the duplicate linked list 21 → - 15 → - 7 and the deleted linked list - 15 → 15.

### Input format:

Enter the address of the first node of L given in the first line and a positive integer N (≤ 105, the total number of nodes). The address of a node is a non negative 5-bit integer, and the NULL address is represented by − 1.

Then N lines, each describing a node in the following format:

Address key value next node

The address is the address of the node, the key value is an integer whose absolute value does not exceed 104, and the next node is the address of the next node.

### Output format:

First output the linked list after de duplication, and then output the deleted linked list. Each node occupies one line and is output according to the input format.

### Input sample:

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

### Output example:

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

The linked list hasn't started to learn, so first use the array method to do this problem, which is written with reference to the ideas of Liu Shen and others

Train of thought 1

This question can be regarded as a query question

1. Define a structure array to store the data of the linked list, and take the address of each node as the index of the array

2. Define vector < int > V1 and V2 to store the node address (array subscript) of the de duplicated and deleted linked list respectively

3. Define an array flag. First initialize all arrays to 0. When traversing the linked list, the linked list starts from the address of the first node given by the title, and the address cannot be empty, that is, the address cannot be - 1, and the address of the current node is equal to the address of the next node; If flag [ABS (key value of current node)] = = 0, then flag [ABS (key value of current node)] = 1 at this time, and put the current node address into v1, otherwise put it into v2

4. Output according to the requirements of the topic

AC code

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <vector> #include <cmath> using namespace std; const int MAXN = 100000; struct node { int data, next; }nodep[MAXN]; int flag[MAXN] = { 0 }; int main() { int node1, n; cin >> node1 >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; cin >> nodep[temp].data >> nodep[temp].next; } memset(flag, 0, sizeof(flag)); vector<int>v1, v2; for (int i = node1; i != -1; i = nodep[i].next) { if (flag[abs(nodep[i].data)] == 0) { flag[abs(nodep[i].data)] = 1; v1.push_back(i); } else { v2.push_back(i); } } printf("%05d %d ", v1[0], nodep[v1[0]].data); for (int i = 1; i < v1.size(); i++) { printf("%05d\n%05d %d ", v1[i], v1[i], nodep[v1[i]].data); } printf("-1\n"); if (v2.size()) { printf("%05d %d ", v2[0], nodep[v2[0]].data); for (int i = 1; i < v2.size(); i++) { printf("%05d\n%05d %d ",v2[i], v2[i], nodep[v2[i]].data); } printf("-1\n"); } }

Liu Shen thought

Original link: L2-002. Linked list de duplication - PAT group programming ladder race GPLT_ Liu Yi blog CSDN blog

1. Use the structure array to store the linked list. The size is MAXN = 100000. Similarly, nodep[i] represents the node with address I

Define a num variable, initialize the num variable to 2 * MAXN, and change the order of the linked list by changing the value of the num variable and finally sort.

2. Mark the num of the node not deleted as cnt1, which indicates the number of nodes not deleted at present; The node to be deleted is marked as MAXN + cnt2. cnt2 indicates the number of nodes currently deleted, because num is initialized as 2 * MAXN at the beginning

So we sort: num in [0,MAXN) is the node that will not be deleted, and num in [MAXN, 2 * MAXN) is the node that will be deleted

num = 2 * MAXN is an invalid node

3. After sorting, the nodes will be sorted according to the order of output. num=[0,cnt1) is the node that will not be deleted, num=[cnt1,cnt1+cnt2) is the deleted node, and num=[cnt1+cnt2, 2 * MAXN) is the invalid node. We only need to output these cnt1+cnt2 nodes

AC code

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cmath> using namespace std; const int MAXN = 100000; int flag[MAXN] = { 0 }; struct node { int address, data, next, num; }nodep[MAXN]; bool cmp(node a, node b) { return a.num < b.num; } int main() { int node1, n; cin >> node1 >> n; for (int i = 0; i < MAXN; i++) { nodep[i].num = 2 * MAXN; } int temp; for (int i = 0; i < n; i++) { cin >> temp; cin >> nodep[temp].data >> nodep[temp].next; nodep[temp].address = temp; } int cnt1 = 0, cnt2 = 0; for (int i = node1; i != -1; i = nodep[i].next) { if (flag[abs(nodep[i].data)] == 0) { flag[abs(nodep[i].data)] = 1; nodep[i].num = cnt1; cnt1++; } else { nodep[i].num = MAXN + cnt2; cnt2++; } } sort(nodep, nodep + MAXN, cmp); for (int i = 0; i < cnt1 + cnt2; i++) { if (i != cnt1 - 1 && i != cnt1 + cnt2 - 1) { printf("%05d %d %05d\n", nodep[i].address, nodep[i].data, nodep[i + 1].address); } else { printf("%05d %d -1\n", nodep[i].address, nodep[i].data); } } return 0; }