PAT1074. Reversing Linked List

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case.  For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed.  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 Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list.  Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
思路:这道题我花了好长时间去解析,为什么花了好长时间, 首先没有读懂题,其次在没读懂题的基础上纠结了好长时间就是临时变量的问题。最后再读懂题的基础上没有考虑边界的情况。总之:受益匪浅

1:这个是错误的代码: 错误的代码必须一辈子铭记。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 #define MAX 100010
 5 struct Info
 6 {
 7     int pre;
 8     int data;
 9     int next;
10 }node[MAX];
11 //一定要仔细读题....every k elements
12 int main(int argc, char *argv[])
13 {
14     int start,N,K;
15     scanf("%d%d%d",&start,&N,&K);
16     for(int i=0;i<N;i++)
17     {
18         int pre,data,next;
19         scanf("%d%d%d",&pre,&data,&next);
20         node[pre].data=data;
21         node[pre].next=next;
22     }
23     int pt=start;
24     //进行前驱记录
25     node[pt].pre=-2;//开头的前驱代表着开始
26     while(node[pt].next!=-1)
27     {
28         int temp=pt;
29         pt=node[pt].next;
30         node[pt].pre=temp;
31     }
32     //寻找第K个
33     pt=start;
34     for(int i=1;i<K&&node[pt].next!=-1;i++)
35     {
36         pt=node[pt].next;
37     }
38     //进行反转
39     int temp=node[pt].next;
40     start=pt;
41     while(node[pt].pre!=-2)
42     {
43         node[pt].next=node[pt].pre;
44         pt=node[pt].pre;
45     }
46     node[pt].next=temp;
47     //进行结果的输出
48     pt=start;
49     while(node[pt].next!=-1)
50     {
51         printf("%05d %d %05d\n",pt,node[pt].data,node[pt].next);
52         pt=node[pt].next;
53     }
54     printf("%05d %d %d\n",pt,node[pt].data,node[pt].next);
55     return 0;

2:正确的代码

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 #define MAX 100010
 5 struct node
 6 {
 7     int add;
 8     int data;
 9     int next;
10 };
11 int main()
12 {
13     vector<node>in(MAX); //像数组一样
14     vector<node>srt;
15     vector<node>out;
16     int start,N,K;
17     scanf("%d%d%d",&start,&N,&K);
18     for(int i=0;i<N;i++)
19     {
20         node temp;
21         scanf("%d%d%d",&temp.add,&temp.data,&temp.next);
22         in[temp.add]=temp;
23     }
24     int next=start;
25     while(next!=-1)
26     {
27         srt.push_back(in[next]);
28         next=in[next].next;
29     }
30     int left=0;
31     while(left<srt.size())
32     {
33
34         for(int i=left+K-1;i>=left;i--)
35         {
36             if(i>=srt.size())
37               break;
38             out.push_back(srt[i]);
39         }
40         left+=K;
41     }
42     if(left!=srt.size())
43        left-=K;
44     while(left<srt.size())
45     {
46         out.push_back(srt[left]);
47         left++;
48     }
49     /*
50     int len=srt.size();
51     int right=K-1;
52     while(right<len)
53     {
54        for(int i=right;i>right-K;i--)
55        {
56            out.push_back(srt[i]);
57        }
58           right+=K;
59     }
60     right-=K;
61     right++;
62     for(int i=right;i<srt.size();i++)
63     {
64           out.push_back(srt[i]);
65     }*/
66     for(int i=0;i<out.size();i++)
67     {
68         if(i==out.size()-1)
69           printf("%05d %d -1\n",out[i].add,out[i].data);
70         else
71           printf("%05d %d %05d\n",out[i].add,out[i].data,out[i+1].add);
72     }
73     return 0;
74 }

时间: 2024-10-14 13:55:40

PAT1074. Reversing Linked List的相关文章

PAT1074 Reversing Linked List (25)详细题解

02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6,

Reversing Linked List

题目来源:PTA02-线性结构2 Reversing Linked List   (25分) Question:Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; i

[PAT] 02-线性结构2 Reversing Linked List(单向链表的逆转) - C语言实现

今天突然想起自己的cnblog有差不多一年没更了??放一道很久前做的也写好了很久但是一直忘记发布的题.如果有不同的算法欢迎分享~ [PAT]02-线性结构2 Reversing Linked List   (25分) Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL bein

PAT 1074. Reversing Linked List (25)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6. Input Specifica

pat02-线性结构1. Reversing Linked List (25)

02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→

02-线性结构3 Reversing Linked List

02-线性结构3 Reversing Linked List(25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every Kelements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must outp

PAT 1074 Reversing Linked List[链表][一般]

1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you mus

02-线性结构3 Reversing Linked List (25 分)

02-线性结构3 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must ou

02-线性结构2 Reversing Linked List

由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4,