# stem

A linked list contains a series of structures, which are not adjacent in memory. Let's assume that each structure has an integer value key and a pointer to the next structure next. Now I will give you a linked list, which is arranged in ascending order of key values

## Input conditions

In the first line, there is a positive integer N[0,10^ 5] (the soul of my big array burns when I see 10 ^ 5). It is the node always, the address is 5 bits (the big array can't stop) and the NULL is - 1. It ensures that the key doesn't repeat and has no ring
The format of the next N lines is

### Example 1

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

## output condition

Output in key ascending order by modifying next (same format as input)

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

# Problem solving

## for the first time

### thinking

There is a very disgusting place in this question
The list he gave may have redundant nodes or a NULL at the beginning
Before the list operation, you need to use a linklist (static list) to save all nodes, and then traverse the header node he gave, find the node behind the node and insert it into the list
Storing all nodes in a structure array list
Arrange the array in ascending order according to the key value
In this case, add example 1 and it will be as follows:
12345 -1 33333
00001 0 22222
11111 100 -1
22222 1000 12345
33333 100000 11111
Then we just need to change the next of the ith to the address of i+1. At the end, next is given to - 1

nlogn

### Writing time

30 minutes (wrong once, I don't know the meaning of trying to use the stem and the small details in the test case)

### code

#### CPP

```#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef struct node
{
int key;
int next;
} node;
bool cmp(node a, node b)
{
return a.key < b.key ? true : false;
}

int main(int argc, char const *argv[])
{
//input
int N;
int temp1, temp2, temp3;
int nowNodeId;
scanf("%d%d", &N, &nowNodeId);
for (int i = 0; i < N; i++)
{
scanf("%d%d%d", &temp1, &temp2, &temp3);
}

//process
vector<node> list;
int nodeCounter=0;
if (nowNodeId==-1)
{
printf("0 -1");
return 0;
}else{
++nodeCounter;
{
++nodeCounter;
}
sort(list.begin(), list.begin() + nodeCounter, cmp);
for (int i = 0; i < nodeCounter - 1; i++)
{
}
list[nodeCounter - 1].next = -1; //Because I typed - 1 directly below, and I could not write this step

//output
printf("%d %05d\n", nodeCounter, nowNodeId);
for (int i = 0; i < nodeCounter - 1; i++)
{
printf("%05d %d %05d\n", list[i].ad, list[i].key, list[i].next);
}
printf("%05d %d -1", list[nodeCounter - 1].ad, list[nodeCounter - 1].key);
}
return 0;
}
```

# ending

I've written so many comments that I can give me a compliment. Please make me angry@
Please also pay attention to my CSDN account. In the next two months, I should update all PTA class a topics in this format

Posted on Tue, 09 Jun 2020 01:46:06 -0400 by fuii_koh