L2-022. 重排链表
2018-03-18 10:15
761 查看
L2-022. 重排链表
时间限制500 ms内存限制65536 kB
代码长度限制8000 B
判题程序Standard作者陈越
给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→...。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。输入格式:每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (<= 105)。结点的地址是5位非负整数,NULL地址用-1表示。接下来有N行,每行格式为:Address Data Next其中Address是结点地址;Data是该结点保存的数据,为不超过105的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。输出格式:对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。输入样例:
00100 6 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218输出样例:
68237 6 00100 00100 1 99999 99999 5 12309 12309 2 00000 00000 4 33218 33218 3 -1这道题目的链表构造其实挺像前向星的(前向星就是模拟链表的一种结构)
需要注意的一点这道题目输入是有冗余数据(即存在不是这个链表的节点),应该是测试点3#include<iostream>
#include<cstdio>
using namespace std;
struct list
{
int data,next;
}node[100005];
int an[100005][2]={0};
int main()
{
int sta,n,cnt=0;
cin >> sta >> n;
for(int i=0;i<n;i++)
{
int add,dat,nex;
cin >> add >> dat >> nex;
node[add].data=dat;
node[add].next=nex;
}
for(int i=sta;i!=-1;i=node[i].next)
{
an[cnt][0]=i;
an[cnt++][1]=node[i].data;
}
int left=0,right=cnt-1;
while(1)
{
printf("%05d %d ",an[right][0],an[right][1]);
right--;
if(left>right)
{
cout << -1;
break;
}
else
printf("%05d\n",an[left][0]);
printf("%05d %d ",an[left][0],an[left][1]);
left++;
if(left>right)
{
cout << -1;
break;
}
else
printf("%05d\n",an[right][0]);
}
return 0;
}
相关文章推荐
- L2-022. 重排链表
- L2-022. 重排链表
- 团体程序设计天梯赛-练习集 -- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- PAT 天梯赛 L2-022. 重排链表 【数据结构】
- L2-022. 重排链表
- 天梯赛L2-022【重排链表】
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- 团体程序设计天梯赛L2-022 重排链表
- PAT L2-022 重排链表 (deque双向队列)
- L2-022. 重排链表(变型)
- 重排链表 未完待续 L2-022
- L2-022. 重排链表
- L2-022. 重排链表
- 天梯赛 L2-002 链表去重 和 L2-022 重排链表
- L2-022. 重排链表