Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Java List problem solution

YASH PAL, 31 July 2024

In this HackerRank java List problem in the java programming language you have Given a list, L, of N integers, perform Q queries on the list. Once all queries are completed, print the modified list as a single line of space-separated integers.

HackerRank Java List problem solution

HackerRank Java List problem 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) {
        final Scanner in = new Scanner(System.in);
        final int size = in.nextInt();
        final List<Integer> list = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            list.add(in.nextInt());
        }
        final int commandCount = in.nextInt();
        for (int i = 0; i < commandCount; i++) {
            in.nextLine();
            String command = in.nextLine();
//            System.out.println("command: " + command);
            if (command.equals("Insert")) {
                int index = in.nextInt();
//                System.out.println("index: " + index);
                int value = in.nextInt();
//                System.out.println("value: " + value);
                list.add(index, value);
            } else {
                int index = in.nextInt();
                list.remove(index);
            }
        }
        int count = 0;
        for (Integer number : list) {
            System.out.print(number);
            if (count != list.size() - 1) {
                System.out.print(" ");
            }
            count++;
        }
    }
}

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) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        List<Integer> list = new LinkedList<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while (n-- > 0) {
            int v = sc.nextInt();
            list.add(v);
        }
        int m = sc.nextInt();
        while (m-- > 0) {
            String s = sc.next();
            if (s.equals("Insert")) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                list.add(a, b);
            } else {
                int x = sc.nextInt();
                list.remove(x);
            }
        }
        for (Integer i : list) {
            System.out.print(i+" ");
        }
        
    }
}

The solution in java8 programming

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List L = new ArrayList();
        for(int i = 0; i < N; i++)
            L.add(i,sc.nextInt());
        int Q = sc.nextInt();
        sc.nextLine();
        for(int i = 0; i < Q; i++) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            if(s1.equalsIgnoreCase("Insert")){
                String arr[] = s2.split(" ");
                int x = Integer.parseInt(arr[0]);
                int y = Integer.parseInt(arr[1]);
                L.add(x,y);
            }
            if(s1.equalsIgnoreCase("Delete")){
                int x = Integer.parseInt(s2);
                L.remove(x);
            }
        }
        Iterator itr = L.iterator();
        while(itr.hasNext()){
            int x = (int)itr.next();
            System.out.print(x + " ");
        }
        sc.close();
    }
}
coding problems hackerrank solutions java

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes