PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

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 (≤) which is the total number of nodes, and a positive K (≤) 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

 

题意:

给N个链表结点,以及K,对每K个长度的链表做逆置,输出逆置后的链表。

题解:

不是很熟悉vector.reverse()这种工具,用了两个双端队列和一个栈来实现。最后一个测试点没过,原来是有些节点不在链表上,那么需要重新计算节点个数,加个计数器sum,不一定就是n。

AC代码:

#include<iostream>
#include<algorithm>
#include<deque>
#include<stack>
using namespace std;
struct node{
    int v;
    int zhi;
    int nx;
}a[100005];
deque<node>q1,q2;
stack<node>st;
int main(){
    int root,n,k;
    cin>>root>>n>>k;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        cin>>a[x].v>>a[x].nx;
        a[x].zhi=x;//把它自己的编号也要记录下来
    }
    int sum=0;//可能有些节点不在链表上,要重新数
    int p=root;
    q1.push_back(a[p]);
    sum++;
    while(a[p].nx!=-1){
        int next=a[p].nx;
        q1.push_back(a[next]);
        sum++;
        p=next;
    }
    for(int i=1;i<=sum/k;i++){
        int c=0;
        while(!q1.empty()){//k个k个分别装入栈里倒一倒再取出来
            node x=q1.front();
            q1.pop_front();
            st.push(x);
            c++;
            if(c==k) break;
        }
        while(!st.empty()){//倒着再取出来
            q2.push_back(st.top());
            st.pop();
        }
    }
    while(!q1.empty()){
        node x=q1.front();
        q1.pop_front();
        q2.push_back(x);
    }
    while(!q2.empty()){//输出
        node x=q2.front();
        q2.pop_front();
        if(!q2.empty()) printf("%05d %d %05d\n",x.zhi,x.v,q2.front().zhi);
        else printf("%05d %d -1",x.zhi,x.v);    
    }
    return 0;
}

 

别人的代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
struct node{
    int add,data,Next;
}a[maxn];
vector<node>valid,ans;
int head,n,k;
int main()
{
    for(int i=0;i<maxn;i++)a[i].add=i;
    scanf("%d%d%d",&head,&n,&k);
    for(int i=0;i<n;i++)
    {
        int address;
        scanf("%d",&address);
        scanf("%d%d",&a[address].data,&a[address].Next);
    }
    int p=head;
    while(p!=-1)
    {
        valid.push_back(a[p]);
        p=a[p].Next;
    }
    int group=valid.size()/k;
    for(int i=0;i<group;i++)
    {
        reverse(valid.begin()+i*k,valid.begin()+i*k+k);
    }
    for(int i=0;i<valid.size();i++)
    {
        if(i!=valid.size()-1)printf("%05d %d %05d\n",valid[i].add,valid[i].data,valid[i+1].add);
        else printf("%05d %d -1\n",valid[i].add,valid[i].data);
    }
    return 0;
} 

 

 

posted on 2019-11-18 22:28  蔡军帅  阅读(485)  评论(0编辑  收藏  举报