-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversing Linked List
67 lines (67 loc) · 1.38 KB
/
Reversing Linked List
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
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#define N 100000
struct Node{
int Data;
int Next;
};
int main()
{
struct Node data[N];
int Ini;
scanf("%d",&Ini);
int n,k;
scanf("%d %d",&n,&k);
int i,pos,Pos[N];
for(i=0;i<n;i++)
{
scanf("%d",&pos);
scanf("%d",&data[pos].Data);
scanf("%d",&data[pos].Next);
}
int j=0;
for(i=Ini;i!=-1;i=data[i].Next)
{
Pos[j++]=i;
}
int rem=j;
n=rem;
i=1;
do{
if(rem>=k)
{
j=i++*k-1;
while(j>=((i-2)*k)&&j>=0)
{
if(rem==n)
{
printf("%05d %d",Pos[j],data[Pos[j]].Data);
rem--;
}else
{
printf(" %05d\n%05d %d",Pos[j],Pos[j],data[Pos[j]].Data);
rem--;
}
j--;
}
}else
{
j+=(k+1);
while(j<n)
{
if(rem==n)
{
printf("%05d %d",Pos[j],data[Pos[j]].Data);
rem--;
}else
{
printf(" %05d\n%05d %d",Pos[j],Pos[j],data[Pos[j]].Data);
}
j++;
}
break;
}
}while(1);
printf(" -1");
return 0;
}