We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Practice
- Java
- Data Structures
- Java List
- Discussions
Java List
Java List
RodneyShag + 0 comments Java solution - passes 100% of test cases
Use a LinkedList instead of an ArrayList when adding/removing items often since it will be faster (ArrayList often needs to shift O(n) elements after an add or remove, which is slow).
From my HackerRank solutions.
import java.util.Scanner; import java.util.LinkedList; public class Solution { public static void main(String[] args) { /* Create and fill Linked List of Integers */ Scanner scan = new Scanner(System.in); int N = scan.nextInt(); LinkedList<Integer> list = new LinkedList<>(); for (int i = 0; i < N; i++) { int value = scan.nextInt(); list.add(value); } /* Perfrom queries on Linked List */ int Q = scan.nextInt(); for (int i = 0; i < Q; i++) { String action = scan.next(); if (action.equals("Insert")) { int index = scan.nextInt(); int value = scan.nextInt(); list.add(index, value); } else { // "Delete" int index = scan.nextInt(); list.remove(index); } } scan.close(); /* Print our updated Linked List */ for (Integer num : list) { System.out.print(num + " "); } } }
Let me know if you have any questions.
lahouari + 0 comments Hi hackers, here is my java code..
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); List<Integer> lst = new ArrayList<>(); for (int i = 0; i < N; i++) { lst.add(sc.nextInt()); } int Q = sc.nextInt(); while (Q-- > 0) { switch (sc.next()) { case "Insert": lst.add(sc.nextInt(), sc.nextInt()); break; case "Delete": lst.remove(sc.nextInt()); break; default: System.out.println("Invalid operation."); System.exit(-1); break; } } sc.close(); while (!lst.isEmpty()) { System.out.print(lst.remove(0) + " "); } }
pradeepgupta30 + 0 comments import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc =new Scanner(System.in); ArrayList<Integer> l = new ArrayList<Integer>(); int n = sc.nextInt(); for(int i=0;i<n;i++) { l.add(i,sc.nextInt()); } int q = sc.nextInt(); for(int i=0;i<q;i++) {String s=sc.next(); if(s.equals("Insert")) { int x = sc.nextInt(); int y = sc.nextInt(); l.add(x,y); } else if(s.equals("Delete")) { int x = sc.nextInt(); l.remove(x); } } Iterator it = l.iterator(); while(it.hasNext()) { Integer ir = (Integer)it.next(); System.out.print(ir+" ");} } }
SnapshotsOfMinds + 0 comments It isn't the most efficient because let's face it, I'm purposely using streams just to get practice with them. Here is a Java 8 solution that passes the tests.
public static void main(String... args) { try (Scanner sc = new Scanner(System.in)) { // Skip since it isn't used due to streams. sc.nextLine(); List<Integer> input = Stream .of(sc.nextLine().split(" ")) .map(Integer::parseInt) .collect(Collectors.toList()); int numQueries = sc.nextInt(); while (numQueries-- > 0) { String query = sc.next(); if (query.equalsIgnoreCase("insert")) { input.add(sc.nextInt(), sc.nextInt()); } else if (query.equalsIgnoreCase("delete")) { input.remove(sc.nextInt()); } } // Printing this way avoids having the extra // space at the end of the string. System.out.println( input.stream() .map(Object::toString) .collect(Collectors.joining(" "))); } }
kavin28 + 0 comments Yeah, My solution can be improved with the use of LinkedList instead of ArrayList, I think for these test cases it's enough. Using List because both classes => Linkedlist & ArrayList are implemented from the list interface.
public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); int n = s.nextInt(); for(int i=0;i<n;i++) list.add(s.nextInt()); int q = s.nextInt(); for(int j=0;j<q;j++) { String op = s.next(); if(op.compareTo("Insert")==0) { int pos = s.nextInt(); int val = s.nextInt(); list.add(pos,val); } else { int pos = s.nextInt(); list.remove(pos); } } for(int k=0;k<list.size();k++) System.out.print(list.get(k)+" "); } }
Load more conversations
Sort 256 Discussions, By:
Please Login in order to post a comment