java – Increases the efficiency of using the linked list

I have a mission to solve this problem. There are a total of 18 test files, of which 5 have failed due to exceeding a time limit in the script and I am unable to check if there are logical errors thereafter .

Description of the question

Mr. Panda is a focus group leader and sets up a simple icebreaker game. At first, N students sit in a circle, one of them holding a ball. Whenever Mr. Panda shouts "NEXT", the student holding the ball passes it to the next student in a clockwise direction. The student receiving the ball will have to shout his name.

From time to time, the student holding the ball may want to "leave" the circle (eg going to the bathroom). In this case, the student will pass the ball to the next pupil of the circle in a clockwise direction and will leave. The student receiving the ball will also shout his name.

New students can also arrive and want to "join" the circle. In this case, the new student will sit in the desired position to be the next student to receive the ball. The ball will then be immediately transmitted to the new student and he will have shouted his name.

As the number of students increases, Mr. Panda has a hard time keeping track of all the students and their names. So, given the list of events that occur (either NEXT, LEAVE, or JOIN), he wants you to code a program to tell him which name will be shouted each time.

Even if students are sitting in a nonlinear circle, Rar the Cat suggests that an intelligent use of linear data structures Java API would be sufficient to solve this task. (ie you do not need to implement your own data structure for this task.)


The first line contains a single integer N, the number of students initially in the circle.
The second line contains N strings, representing the students' names of the circle in the direction of clockwise. The first name in the list is the name of the student who currently holds the ball.
The third line contains a single integer Q, the number of events that occur.
The following Q lines represent events that occur in chronological order. The first string of each line will be either NEXT, LEAVE or JOIN, representing the event. If the event is JOIN, this line will contain a second string representing the name of the student who is attached.


The output must contain Q lines, representing the names that are shouted in the order of events.


• 3≤N, Q≤200,000

• It is guaranteed that the name of each student will be composed only of English letters and will be at most
10 characters long. However, student names may not be distinct. For example, he
may have more than one student with the name "Bob".

• It is also guaranteed that there will be at least 3 people in the circle at all times.

Example of input

Alice Bob Charlie
JOIN Donald

Sample output


import java.util. *;

public class Ballpassing {
private void run () {
// implement your "main" method here
LinkedList lst = new LinkedList <> ();
Scanner sc = new Scanner (;
int length = sc.nextInt ();
for (int i = 0; i <length; i ++) {
String name = ();
lst.add (name);
int idx = 0;
int count = sc.nextInt ();
for (int i = 0; i <number; i ++) {
int nextIdx = (idx + 1)% lst.size ();
String cmd = ();
if (nextIdx == 0) {
LinkedList shifted = (LinkedList) lst.clone ();
String element = shifted.getLast ();
shifted.remove (idx);
shifted.addFirst (element);
idx = 0;
nextIdx = 1;
lst = offset;


if (cmd.equals ("LEAVE")) {
System.out.println (lst.get (nextIdx));
lst.remove (idx);
Carry on;
} else if (cmd.equals ("JOIN")) {
String name = ();
lst.add (nextIdx, name);
idx = nextIdx;
System.out.println (lst.get (idx));


public static void main (String[] args) {
Ballpassing newBallpassing = new Ballpassing (); ();