题解:直接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 = + ;
//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, , n) {
int x;
cin >> x;
cin >> val[x] >> nxt[x]; } for (int p = head; p != -; p = nxt[p]) {
l.push_back(mp(p, val[p]));
} list<pair<int, int> >::iterator it1,it2;
it2 = l.end(); it2--;
if (n % == ) {
for (it1 = l.begin();;) {//
ll.push_back(*it2); n--; if (n == )break;
it2--; //if (it1 == it2)break;
ll.push_back(*it1); n--; if (n == )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\n", it1->first);//cout<< it->first << endl;
else cout << - << endl;
} system("pause"); }
/*
00100 3 23854 2 1
1 3 -1
00100 1 23854
*/

CCCC L2-022. 重排链表的更多相关文章

  1. L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  2. pat甲级 团体天梯赛 L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  3. GPLT天梯赛 L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  4. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...

  5. Java实现 LeetCode 143 重排链表

    143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...

  6. 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释

    题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...

  7. L2-022 重排链表 (25 分)

    L2-022 重排链表 (25 分)   给定一个单链表 L​1​​→L​2​​→⋯→L​n−1​​→L​n​​,请编写程序将链表重新排列为 L​n​​→L​1​​→L​n−1​​→L​2​​→⋯.例 ...

  8. LeetCode 重排链表 OPPO笔试

    重排链表 几个关键点: 1. 双指针(快慢指针找中点)(用于反转后一部分) 2. 反转后一部分 (reverse函数) 3. 合并链表 合并的时候在笔试的时候想了一种比我之前想的简单的方法 从slow ...

  9. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

随机推荐

  1. Java求解汉诺塔问题

    汉诺塔问题的描述如下:有3根柱子A.B和C,在A上从上往下按照从小到大的顺序放着一些圆盘,以B为中介,把盘子全部移动到C上.移动过程中,要求任意盘子的下面要么没有盘子,要么只能有比它大的盘子.编程实现 ...

  2. 2. Oracle体系结构

    一. Oracle体系结构图 体系结构图如下: 体系结构图看似简单:其中很有玄机.它对Oracle来讲:相当于武功中的心法.配合体系结构来学Oracle:能起到事半功倍的效果.我们平时遇到Oracle ...

  3. 手机CPU

    说起手机CPU的历史,笔者给大家提一个问题:"世界上第一款智能手机是什么呢?"相信很多人的答案是爱立信的R380或诺基亚的7650,但都不对,真正的首款智能手机是由摩托罗拉在200 ...

  4. C/C++获取文件后缀名并且比较

    以下这段是VC中过去文件后缀名的方法 1.CString GetSuffix(CString strFileName) {         return strFileName.Right(strFi ...

  5. iOS - Share Extension

    1. 学 Share Extension 之前 先了解一下iOS的App Extension 2.1 创建Share Extension扩展Target ** 注:扩展不能单独创建,必须依赖于应用工程 ...

  6. Oralce分析函数

    1 列传行  listagg(city,',')  within GROUP (order by city)    over (partition by nation) rank with temp ...

  7. 13条Android手机必备技巧 让玩机更有趣

    腾讯数码讯(编译:张秀梅)如果你不是一名极客或手机爱好者,那么或许对于手中的Android手机来说,肯定无法做到百分之百了解.对于这款世界上最受欢迎的操作系统来说,有许多不为大部分人所知晓的使用技巧或 ...

  8. python tkinter学习——布局

    目录 一.pack() 二.grid() 三.place() 四.Frame() 正文 布局 一.pack() pack()有以下几个常用属性: side padx pady ipadx ipady ...

  9. 题目1446:Head of a Gang(并查集操作)

    题目链接:http://ac.jobdu.com/problem.php?pid=1446 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  10. 题目1002:Grading(题目背景基于高考打分的简单判断)

    题目链接:http://ac.jobdu.com/problem.php?pid=1002 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...