Scanner not working

import java.util.*;
public class Main {

private static Node<Integer> insert(int n, Scanner s){
	Node<Integer> head = null;
	Node<Integer> tail = null;
	int val = s.nextInt();

	int size = 0;
	while(size < n){
		Node<Integer> newNode = new Node<>(val);
		if(head == null){
			head = newNode;
			tail = newNode;
		} else{
			tail.next = newNode;
			tail = tail.next;
		}
		if(size < n)
		val = s.nextInt();
		size++;
	}
	return head;
}

public static void printLL(Node<Integer> head){
    while (head != null){
        System.out.print(head.val + " ");
        head=head.next;
    }
}

private static Node<Integer> kthFromTheEnd(Node<Integer> head, int n, int k){
	int posFromStart = n - k - 1;
	Node<Integer> temp = head;
	int index = 0;
	while(index < posFromStart){
		temp = temp.next;
		index++;
	}

	temp.next = temp.next.next;
	return head;
}
public static void main (String args[]) {
	Scanner s = new Scanner(System.in);
	int size = s.nextInt();
	int k = s.nextInt();
	Node<Integer> head = insert(size, s);
	printLL(kthFromTheEnd(head, size, k));
}

}

class Node {

T val;
Node<T> next;

public Node(T val){
	this.val = val;
}

}

Exception in thread “main” java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Main.insert(Main.java:20) at Main.main(Main.java:49)

hello @himesh.sharma25

put size++ before the if condition.

check ur updated code here->

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.