Java List HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Java List HackerRank Solution. This problem is a part of the HackerRank Java Programming Series.

Java List HackerRank Solution
Java List HackerRank Solutions

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

Java List HackerRank Solutions

Ezoicreport this adProblem

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 HackerRank Solutions

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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Java Map HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad