1074 Reversing Linked List (25 分)

开始写了一版,debug了半天,没有找到问题,始终有两个点过不去。

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.

emmmmm, 然后发现是题读错了,能读错也是佩服我自己,题目说的是every K elements。

写了半天就莫名其妙理解成前K个。

ps.stl中的reverse是真的好用。

所以还是得熟悉stl操作啊,速度快省心方便,不用白不用啊。

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int _val[100005];
int _next[100005];

vector p;

int main()
{
    int start, n, k;
    //scanf("%d%d%d", &start, &n, &k);
    cin>>start>>n>>k;


    for(int i=0;i>a>>v>>ne;
        _next[a] = ne;
        _val[a] = v;
    }

    int cnt = 0;
    int address = start;
    while(address != -1)
    {
        cnt++;
        p.push_back(address);
        address = _next[address];
    }

    int gg = cnt/k;
    for(int i=0;i::iterator it = p.begin(); it != p.end(); ++it)
    {
        if(it == p.end()-1)
        {
            printf("%05d %d -1\n", *it, _val[*it]);
        }
        else
            printf("%05d %d %05d\n", *it, _val[*it], *(it+1));
    }

}

 

你可能感兴趣的:(pat)