# PAT链表题汇总 1032 1074 1097 1133

PAT的链表题还算比较友好 ，大多用静态链表

PAT 1032 Sharing

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int N=1e5+233;
struct node {
char data;
int next;
}a[N];
int vis[N];
int main(){

int head,head1,n;
cin>>head>>head1>>n;
rep(i,0,n){
int id;
cin>>id;
cin>>a[id].data>>a[id].next;
}
int p=head;
while(p!=-1){
vis[p]=1;
p=a[p].next;
}
p=head1;
while(p!=-1){
if(vis[p]) {
printf("%05d",p);
return 0;
}
p=a[p].next;
}
puts("-1");
}

PAT1074 Reversing Linked List 反转链表

1、本题中可能包含不在链表中的节点，这些节点无需考虑。

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int N=1e5+233;
struct node {
int data,next;
}a[N];
int L[N],ans[N];
int main(){

int head,n,k;
cin>>head>>n>>k;
rep(i,0,n){
int id;
cin>>id;
cin>>a[id].data>>a[id].next;
}
int cnt=0;
int p=head;
while(p!=-1){
L[cnt++]=p;
p=a[p].next;
}
rep(i,0,cnt) ans[i]=L[i];
rep(i,0,cnt/k){
rep(j,0,k)
ans[i*k+j]=L[i*k+k-1-j];
}
rep(i,0,cnt-1)
printf("%05d %d %05d\n",ans[i],a[ans[i]].data,ans[i+1]);
printf("%05d %d -1\n",ans[cnt-1],a[ans[cnt-1]].data);
}

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int N=1e5+233;
struct node {
int data;
int next;
}a[N];
int vis[N];
int L[N],del[N];
int main(){

int head,n;
cin>>head>>n;
rep(i,0,n){
int id;
cin>>id;
cin>>a[id].data>>a[id].next;
}
int cnt=0,tot=0;
int p=head;
while(p!=-1){
int t=fabs(a[p].data);
if(!vis[t]){
vis[t]=1;
L[cnt++]=p;
}
else{
del[tot++]=p;
}
p=a[p].next;
}
rep(i,0,cnt-1)
printf("%05d %d %05d\n",L[i],a[L[i]].data,L[i+1]);
printf("%05d %d -1\n",L[cnt-1],a[L[cnt-1]].data);
if(tot){
rep(i,0,tot-1)
printf("%05d %d %05d\n",del[i],a[del[i]].data,del[i+1]);
printf("%05d %d -1\n",del[tot-1],a[del[tot-1]].data);
}
}

PAT1133 Splitting A Linked List 链表元素分类

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int N=1e5+233;
struct node {
int data;
int next;
}a[N];

int L[4][N];

int main(){
int head,n,k;
cin>>head>>n>>k;
rep(i,0,n){
int id;
cin>>id;
cin>>a[id].data>>a[id].next;
}
int cnt[4]={0};
int p=head;
while(p!=-1){
int t=a[p].data;
if(t<0){
L[1][cnt[1]++]=p;
}
else if(t<=k)
L[2][cnt[2]++]=p;
else
L[3][cnt[3]++]=p;
p=a[p].next;
}
vector<int>ans;
rep(i,1,4)
rep(j,0,cnt[i])
ans.push_back(L[i][j]);
rep(i,0,ans.size()-1){
printf("%05d %d %05d\n",ans[i],a[ans[i]].data,ans[i+1]);
}
printf("%05d %d -1\n",ans[ans.size()-1],a[ans[ans.size()-1]].data);

}

12-06 341

07-13 8298

02-03 7889

03-19 18

08-02 1559

09-24 253

10-16 652

08-06 7508

03-12 552

05-20 14

04-09 18

09-28 39万+

07-25 5468

12-02 231

08-08 3286

12-28 1543

03-16 191

02-02 153

08-15 2432

04-14 60万+

03-13 15万+

02-19 18万+

03-01 13万+

03-04 14万+

03-06 3728

03-08 7万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 11万+

03-13 11万+

03-16 2871

03-18 1万+

03-19 8万+

03-19 4万+

03-20 1万+

03-22 5万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-30 4万+

03-26 3022

03-29 22万+

04-10 1955

03-29 10万+

03-30 17万+

04-17 5万+

04-01 3390

03-23 1万+

04-02 1万+

04-02 4万+

06-01 865

04-06 7万+

04-06 5027

04-09 8万+

04-09 3万+

05-17 9800

04-11 4万+

04-15 6万+

04-18 5万+

04-20 5万+

05-03 1万+

05-16 5万+

05-06 2万+

05-07 1万+

05-08 4万+

05-12 1万+

05-14 7505

05-16 2476

05-16 1万+

05-16 2万+

05-30 2766

05-18 9322

05-18 6037

05-18 4374

05-21 9003

05-21 9170

#### 老码农吐血建议：2020年，低于1w的程序员要注意了...

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客