1074. Reversing Linked List (25)
来源:互联网 发布:ios软件开发教程 编辑:程序博客网 时间:2024/05/17 22:07
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:00100 6 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
#include <iostream>#include <vector>#include <algorithm>#include <iomanip>using namespace std;const int MAX = 100000;struct node{int cur_addr;int key;int next_addr;};int main(){ios::sync_with_stdio(false);int head, n, k;cin >> head >> n >> k;vector<node> mem(MAX);node t;for (int i = 0; i < n; i++){cin >> t.cur_addr >> t.key >> t.next_addr;mem[t.cur_addr] = t;}int h = head;int count = 0;vector<node> list;while (head != -1){list.push_back(mem[head]);head = mem[head].next_addr;count++;}int c = count / k;auto itr = list.begin();while (c--){reverse(itr, itr + k);itr += k;}for (int i = 0; i < list.size(); i++){if (i != list.size() - 1)printf("%05d %d %05d\n", list[i].cur_addr, list[i].key, list[i + 1].cur_addr);elseprintf("%05d %d -1\n", list[i].cur_addr, list[i].key);}return 0;}
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- Android Studio 2.0 Instant Run无效(Instant Run没有闪电符号)
- 服务器接收Android客户端中文乱码解决
- 基于Win API,通过算法实现圆与鼠标指针(点)的碰撞的模拟
- 重学OpenGL(一)----工具篇
- HTML标签(按功能排序)
- 1074. Reversing Linked List (25)
- BootStrap学习
- AVR121-使用过采样增加ADC精度
- 正则表达式抓取网页数据 制作iOS小词典
- UIPopoverController的使用
- Building Maintainable Software-java篇之Automate Tests
- 【转】主题模型--pLSA,LDA
- 4.14-2 如何使用Android MediaStore裁剪大图片
- hdu 5470 Typewriter