Java List – Hacker Rank Solution

Java List - Hacker Rank Solution
Java List – Hacker Rank Solution

Problem :

For this problem, we have  types of queries you can perform on a List:

  1. Insert  at index :
    Insert x y
  2. Delete the element at index :
    Delete x

Problem Statement: Click Here.



Java List – Hacker Rank Solution

import java.io.*;
import java.util.*;
public class Solution {
    public static void main(String[] args) {
        List l = new LinkedList();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            l.add(sc.nextInt());
        }
        int q = sc.nextInt();
        for (int i = 0; i < q; i++) {
            String command = sc.next();
            if (command.equals("Insert")) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                l.add(x, y);
            } else if (command.equals("Delete")) {
                int x = sc.nextInt();
                l.remove(x);
            }
        }
        Iterator it = l.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}

Disclaimer: The above Problem (Java List) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment