码迷,mamicode.com
首页 > 其他好文 > 详细

1074. Reversing Linked List (25)

时间:2015-12-06 13:02:39      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

reverse 方法很好用

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

 

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. struct Node {
  8. int adddress;
  9. int value;
  10. int next;
  11. };
  12. vector<Node> list;
  13. int val[100001] = { 0 }, nex[100001] = { 0 };
  14. int main(void) {
  15. int first_addr, n, k;
  16. cin >> first_addr >> n >> k;
  17. if (first_addr == -1) //首地址为-1.直接输出
  18. {
  19. printf("-1\n");
  20. return 0;
  21. }
  22. for (int i = 0; i < n; i++) {
  23. int temp_addr;
  24. cin >> temp_addr;
  25. cin >> val[temp_addr] >> nex[temp_addr];
  26. }
  27. int addr = first_addr;
  28. while (1) {
  29. Node node_temp;
  30. node_temp.adddress = addr;
  31. node_temp.value = val[addr];
  32. node_temp.next = nex[addr];
  33. addr = nex[addr];
  34. list.push_back(node_temp);
  35. if (addr == -1)
  36. break;
  37. }
  38. while (k > list.size()) {
  39. reverse(list.begin(), list.end());
  40. k -= list.size();
  41. }
  42. /*reverse(list.begin(), list.begin() + k);*/
  43. int loop = list.size() / k, i;
  44. for (i = 0; i<loop; ++i)
  45. reverse(list.begin() + i*k, list.begin() + (i + 1)*k);
  46. for (int i = 0; i < list.size()-1; i++) {
  47. list[i].next = list[i + 1].adddress;
  48. }
  49. list[list.size() - 1].next = -1;
  50. for (int i = 0; i < list.size(); i++) {
  51. if (list[i].next != -1)
  52. printf("%05d %d %05d\n", list[i].adddress, list[i].value, list[i].next);
  53. else
  54. printf("%05d %d %d\n", list[i].adddress, list[i].value, list[i].next);
  55. }
  56. return 0;
  57. }





1074. Reversing Linked List (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!