CCCC L2-022. 重排链表

题解:直接list模拟,头尾两个指针,分别将头尾元素push到另一个list里面,输处输入方式同上一篇

坑:第一发卡了第二个样例,第二发卡了第4个,莫名其妙,所以把两个代码合起来,然后强行ac了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)

using namespace std;
const  int N = 100000 + 5;
//double num[N], price[N], ave[N];
int nxt[N], val[N];
int nxt1[N];
map<int, int> p;
list<pair<int, int> >l,ll;
int main() {
    int head, n;
    cin >> head >> n;
    _for(i, 0, n) {
        int x;
        cin >> x;
        cin >> val[x] >> nxt[x];

    }

    for (int p = head; p != -1; p = nxt[p]) {
        l.push_back(mp(p, val[p]));
    }
    
    list<pair<int, int> >::iterator it1,it2;
    it2 = l.end(); it2--;
    if (n % 2 == 1) {
        for (it1 = l.begin();;) {//
            ll.push_back(*it2); n--; if (n == 0)break;
            it2--; //if (it1 == it2)break;
            ll.push_back(*it1); n--; if (n == 0)break;
            it1++;// if (it1 == it2)break;

        }
    }
    else {
        for (it1 = l.begin(); it1 != it2;) {
            ll.push_back(*it2);
            ll.push_back(*it1);

            it1++; if (it1 == it2)break;
            it2--;
        }
    }
    for (it1 = ll.begin(); it1 != ll.end(); ) {
        printf("%05d %d ", it1->first, it1->second);
        //cout << it->first << ' ' << it->second << ' ';
        if (++it1 != ll.end())printf("%05d
", it1->first);//cout<< it->first << endl;
            else cout << -1 << endl;
    }

    system("pause");


}
/*
  00100 3

23854 2 1
1 3 -1
00100 1 23854
*/
成功的路并不拥挤,因为大部分人都在颓(笑)
原文地址:https://www.cnblogs.com/SuuT/p/8668748.html