PAT 1074. Reversing Linked List
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <unordered_map> using namespace std; class Node {
public:
int data;
int next;
Node() : data(), next() {}
Node(int d, int n):data(d), next(n){}
}; int reverse_help(int head, int k, unordered_map<int, Node>& mem) {
if (head == -) {
return -;
} int cur = head;
int prev= -;
while (cur != -) {
if (k == ) {
break;
}
int tmp = mem[cur].next;
mem[cur].next = prev;
prev = cur;
cur = tmp; k--;
}
mem[head].next = cur;
return prev;
} int reverse(int head, int k, unordered_map<int, Node>& mem, int n) {
if (head == -) {
return -;
} int nhead = -;
int cur = head;
int pre = -; while (cur != -) {
int t = reverse_help(cur, k, mem);
if (nhead == -) {
nhead = t;
}
if (pre != -) {
mem[pre].next = t;
}
pre = cur;
cur = mem[cur].next;
n -= k;
if (n < k) {
break;
}
} return nhead;
} void print(int head, unordered_map<int, Node>& mem) {
int cur = head;
while(cur != -) {
Node& cnode = mem[cur];
if (cnode.next != -) {
printf("%05d %d %05d\n", cur, cnode.data, cnode.next);
} else {
printf("%05d %d %d\n", cur, cnode.data, cnode.next);
}
cur = mem[cur].next;
}
} int count(int head, unordered_map<int, Node>& mem) {
int cnt = ;
int cur = head;
while(cur != -) {
cnt++;
cur = mem[cur].next;
}
return cnt;
} int main() { int head, n, k;
scanf("%d%d%d", &head, &n, &k); unordered_map<int, Node> mem; for (int i=; i<n; i++) {
int addr, data, next;
scanf("%d%d%d", &addr, &data, &next);
mem.insert(make_pair(addr, Node(data, next)));
} cout<<"===="<<endl;
print(head, mem);
cout<<"===="<<endl;
head = reverse(head, k, mem, count(head, mem));
print(head, mem); return ;
}
卧槽,敢再无聊点么,输入节点数据竟然有不含在链表里的节点数据。
PAT 1074. Reversing Linked List的更多相关文章
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- PAT 1074. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- PAT Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
- PAT (Advanced Level) 1074. Reversing Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
随机推荐
- 关于字典的几个类--defaultdict,OrderedDict, zip()
一. 1个键对应多个值 比如:d = {'a' : [1, 2, 3], 'b' : [4, 5]},可以使用 collections 模块中的 defaultdict 来构造这样的字典 from ...
- leetcode-733-Flood Fill
题目描述: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
- 实验一 c++简单程序设计
一.实验内容 1.ex 2_28 (1) 用if...else判断 #include<iostream> using namespace std; int main() { char i; ...
- jenkins运行Python
法一: 配置中构建执行Windows批处理命令如下 立即构建后,报错如下,提示python 不是内部或外部指令 修改Windows批处理指令如下: 再次“立即构建”则正常 法二: 安装Python插件 ...
- OpenFoam+CFDEM+Liggghts安装耦合
这里安装的时间节点为:2018.10.29,安装的是目前的最新版本CFDEM,支持到与OpenFoam-5.x的耦合. 1. 先安装openfoam:https://openfoam.org/down ...
- Google Spanner vs Amazon Aurora: Who’ll Get the Enterprise?
https://www.clustrix.com/bettersql/spanner-vs-aurora/ Google Spanner versus Amazon Aurora In July 20 ...
- 牛顿迭代法(Newton's method)
关键词:牛顿法.牛顿迭代法.牛顿切线法.牛顿-拉弗森方法 参考:牛顿迭代法-百度百科.牛顿切线法-百度文库数学学院.牛顿切线法数值分析.非线性方程(组)的数值解法.Latex入门 https://bl ...
- ECharts-入门学习
最近因项目需要要做图表,后台数据要以柱状图的形式展示,通过上网查找,感觉ECharts这个js控件挺不错的,下面就把入门知识搞一下. 一.下载ECharts控件. 地址:http://echarts. ...
- pull强制覆盖本地
今天我总结的是在项目中经常用到的Git命令,上传和下拉文件. 当然在进行上传和下拉操作之前,你首先要做的就是将本地和Git库连接起来. 连接命令: git remote add origin + 你G ...
- linux mint 18.2 install postgresql
https://www.postgresql.org/download/linux/ubuntu/ 1 check Xenial16.04 2 创建文件 /etc/apt/sources.list ...