Input Format

The first line contains an integer,  (the initial number of elements in ).
The second line contains  space-separated integers describing .
The third line contains an integer,  (the number of queries).
The  subsequent lines describe the queries, and each query is described over two lines:

  • If the first line of a query contains the String Insert, then the second line contains two space separated integers , and the value  must be inserted into  at index .
  • If the first line of a query contains the String Delete, then the second line contains index , whose element must be deleted from .

Constraints



  • Each element in is a 32-bit integer.

Output Format

Print the updated list  as a single line of space-separated integers.


hackerrank java list solution


Java List problem solution | HackerRank

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
 public static void main(String[] args) {
  Scanner st = new Scanner(System.in);
  LinkedList < Integer > LL = new LinkedList < Integer > ();
  int n = st.nextInt();
  for (int i = 0; i < n; i++) {
   int p = st.nextInt();
   LL.add(p);
  }
  int q = st.nextInt();
  while (q--> 0) {
   st.nextLine();
   String s = st.nextLine();
   if (s.equals("Insert")) {
    int index = st.nextInt();
    int num = st.nextInt();
    LL.add(index, num);
   } else {
    int num = st.nextInt();
    LL.remove(num);
   }
  }

  Iterator itr = LL.iterator();
  while (itr.hasNext()) {
   System.out.print(itr.next() + " ");
  }
  System.out.println("\n");
 }
}

Second solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
 public static void main(String[] args) {
  Scanner st = new Scanner(System.in);
  int n = st.nextInt();
  assert(n >= 1 && n <= 4000);
  LinkedList L = new LinkedList();
  for (int i = 0; i < n; i++) {
   int x = st.nextInt();
   L.add(i, x);

  }

  int q = st.nextInt();
  assert(q >= 1 && q <= 4000);

  for (int i = 0; i < q; i++) {
   String s = st.next();
   if (s.equals("Insert")) {

	int x = st.nextInt();
	int y = st.nextInt();
	L.add(x, y);
   } else {
	int y = st.nextInt();
	L.remove(y);
   }

  }
  Iterator < Integer > Iterator = L.iterator();
  boolean flag = false;
  while (Iterator.hasNext()) {
   if (flag) System.out.print(" ");
   flag = true;
   System.out.print(Iterator.next());
  }
  System.out.print("\n");
 }
}