Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

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 solutions Hackerrank Problems Solutions Java Programming Tutorials

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes