# PAT Advanced 1097 Deduplication on a Linked List: Solution Not Using Pointers

## Overview

PAT Advanced 1097 Deduplication on a Linked List: herre I give a solution to simulate the memory without using pointers, special cases are highlighted.

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 (<= 105) 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:

where Address is the position of the node, Key is an 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, 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```

## Solution Not Using Pointers

A common solution is to directly use Linked List which typically use pointers, here I allocate a global memory block as a simulation of the physical memory hardware and directly use integer type to serve as the pointer, i.e., the memory address, this helps understand more on the low level machine hardware mechanism the following solution is accepted by PAT OJ to pass this PAT Advanced 1097 Deduplication on a Linked List problem after which I ll highlight special case which could easily result in WA:

```#include <cstdio>
#include <set>

using std::set;

int abs(int n) {
return n < 0 ? -n : n;
}

struct Node {
int val;
int next;
};

Node mem[100005];

void print(int curr) {
while (curr != -1) {
printf("%.5d %d ", curr, mem[curr].val);
if (mem[curr].next != -1) {
printf("%.5d\n", mem[curr].next);
} else {
printf("-1\n");
}
curr = mem[curr].next;
}
}

int main() {

return 0;
}

for (int i = 0; i < n; ++i) {
}

set<int> used;
int h1 = -1;
int c = -1;

int key = 0;
while (curr != -1) {
key = abs(mem[curr].val);
if (used.count(key)) {

if (h1 == -1) {
h1 = curr;
c = curr;
} else {
mem1.next = curr;
c = curr;
}

mem[prev].next = mem[curr].next;
curr = mem[prev].next;

mem1.next = -1;
} else {
prev = curr;
curr = mem[curr].next;
used.insert(key);
}
}

print(h0);
print(h1);

return 0;
}

```

Note some special cases: like there are only two nodes, the first node contains the key of 15 and the second contains the key of -15. The other we need to be careful about is that do not forget to set the tail to -1 of the list.

## Summary

PAT Advanced 1097 Deduplication on a Linked List: herre I give a solution to simulate the memory without using pointers, special cases are highlighted.

Written on June 1, 2015