天梯赛 L3 重排链表

题目链接:点击打开链接

思路:先根据题目信息找到原始链表,然后根据规则重排

#include <bits/stdc++.h>
using namespace std;
int head;
int data[100010],nex[100010];
int main(){
	int n;
	cin >> head >> n;
	for(int i = 0;i < n;i++){
		int h,d,ne;
		cin >> h >> d >> ne;
		data[h] = d;
		nex[h] = ne;
	}
	vector<int> v,re;
	int now = head;
	while(now != -1){
		v.push_back(now);
		now = nex[now];
	}
	int i = 0,j = v.size() - 1;
	while(i <= j){
		re.push_back(v[j--]);
		if(i > j) continue;
		re.push_back(v[i++]);
	}
	for(int i = 0;i < re.size() - 1;i++){
		printf("%05d %d %05d\n",re[i],data[re[i]],re[i + 1]);
	}
	int in = re.size() - 1;
	printf("%05d %d %d\n",re[in],data[re[in]],-1);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ccDLlyy/article/details/79943851
今日推荐