Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int t = input.nextInt();
- LinkedList<Integer> list = new LinkedList<>();
- for (int i = 0; i < t; i++) list.add(input.nextInt());
- t = input.nextInt();
- while (t--> 0) {
- input.nextLine();
- String s = input.nextLine();
- if (s.equals("Insert")) {
- int n = input.nextInt();int q = input.nextInt();
- try {
- if (n >= list.size()) list.add(q);
- else list.set(n,q);
- } catch (Exception e) {
- list.addLast(q);
- }
- continue;
- } else if (s.equals("Delete")) {
- int n = input.nextInt();
- try {
- list.remove(n);
- } catch (Exception e) {
- list.pollLast();
- }
- continue;
- }
- }
- for (int i = 0; i < list.size(); i++) System.out.print(list.get(i) +" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement