-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java List
54 lines (45 loc) · 1.22 KB
/
Java List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Output Format
Print the updated list as a single line of space-separated integers.
Sample Input
5
12 0 1 78 12
2
Insert
5 23
Delete
0
Sample Output
0 1 78 12 23 */
code
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 + " ");
}
}
}