Java List Hacker Rank Solution

Hello coders, In this post, you will learn how to solve Java List Hacker Rank Solution. This problem is a part of the Java programming series. 

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

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.

Sharing Is Caring

Leave a Comment