Ladder Match L3 Rearrangement List

Topic link: Click to open the link

Idea: First find the original linked list based on the title information, and then rearrange it according to the rules

#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] = no;
	}
	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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324849877&siteId=291194637