Why my code take infinite inputs

@deepgarg46 Use the original snippet of the code given.

https://ide.codingblocks.com/s/681529 again same problem

@deepgarg46 bench mark your function with this :

public static void func(LinkedList q, int n, int[] arr) throws Exception {
        int ans = 0;
        for (int j = 0; j < n; j++) {
            if (q.getFirst() == arr[j]) {
                ans++;
                q.removeFirst();
            } else {
                while (q.getFirst() != arr[j]) {
                    int var = q.getFirst();
                    q.addLast(var);
                    q.removeFirst();
                    ans++;
                }
                ans++;
                q.removeFirst();
            }

        }
        System.out.println(ans);

    }

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.