zoukankan      html  css  js  c++  java
  • 1074 Reversing Linked List (25 分)

    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 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

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<set>
    #include<stack>
    #include<string.h>
    #include<cstdio>
    #include <unordered_map>
    #include<cmath>
    
    using namespace std;
    struct Node
    {
        int address,data,next;
    };
    
    int main()
    {
        int head,n,k;
        scanf("%d%d%d",&head,&n,&k);
        map<int,Node> mp;
        //Node temp;
        int a,b,c;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            mp[a]={a,b,c};
        }
        vector<Node>node;
        for(int i=head;i!=-1;i=mp[i].next)
            node.push_back(mp[i]);
        int len=node.size();
        for(int i=0;i<len;i+=k)
        {
            if(len-i<k)
                break;
            int temp=1;
            for(int j=i;j<(i+i+k)/2;j++)
            {
                swap(node[j],node[i+k-temp]);
                temp++;
            }
    
        }
        for(int i=0;i<len-1;i++)
        {
            printf("%05d %d %05d
    ",node[i].address,node[i].data,node[i+1].address);
        }
        printf("%05d %d -1
    ",node[len-1].address,node[len-1].data);
        return 0;
    }
  • 相关阅读:
    ROS_Kinetic_08 ROS的集成开发环境(IDEs)之使用Eclipse
    如何写好一份竞品运营分析报告?
    我眼中的Linux设备树(六 memory&chosen节点)
    【干货】前端开发者最常用的六款IDE
    【前端图表】echarts散点图鼠标划过散点显示信息
    【前端图表】echarts实现散点图x轴时间轴
    三分钟教你学会如何将密文解码成明文
    2018上半年GitHub上最热门的开源项目
    进阶攻略|前端完整的学习路线
    进阶攻略|前端最全的框架总结
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10327740.html
Copyright © 2011-2022 走看看