AcWing 826. 单链表-Java
原题链接
简单
作者:
Susu
,
2020-01-27 21:54:42
,
所有人可见
,
阅读 707
import java.io.*;
public class Main {
static int N = 100010;
static int head;
static int idx;
static int[] e = new int[N];
static int[] ne = new int[N];
static void init(){
head = -1;
idx = 0;
}
static void add(int x){
e[idx] = x;
ne[idx] = head;
head = idx++;
}
static void addK(int x,int k){
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx++;
}
static void remove(){
head = ne[head];
}
static void removeK(int k){
ne[k] = ne[ne[k]];
}
public static void main(String[] args) throws Exception{
init();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
while(n-->0){
String[] strs = br.readLine().split(" ");
if(strs[0].equals("H")){
int x = Integer.valueOf(strs[1]);
add(x);
}else if(strs[0].equals("I")){
int k = Integer.valueOf(strs[1]);
int x = Integer.valueOf(strs[2]);
addK(x,k-1);
}else{
int k = Integer.valueOf(strs[1]);
if(k==0) remove();
else removeK(k-1);
}
}
for(int i=head;i!=-1;i=ne[i]){
System.out.print(e[i]+" ");
}
}
}