1074 Reversing Linked List(25点)

1571 ワード

一版を書き始めたが、debugは長い間、問題が見つからず、ずっと2つの点が過ぎなかった.
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
", *it, _val[*it]); } else printf("%05d %d %05d
", *it, _val[*it], *(it+1)); } }