I have written the following code but output is not coming. Please help.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
LinkedList list = new LinkedList();
while(scn.nextInt() != -1) {
int number = scn.nextInt();
list.addLast(number);
}
int k = scn.nextInt();
System.out.println(list.KNode(k));
}
private class Node {
int data;
Node next;
}
private Node head;// initial node
private Node tail;// last nodes
private int size;// no of nodes added
public void addLast(int item) {
// creation of node
Node nn = new Node();
nn.data=item;
nn.next=null;
// attach of node
if(size >= 1) {
this.tail.next=nn;
}
// updating summary object (head tail and size)
if(size==0) {
this.head = nn;
this.tail=nn;
this.size++;
} else {
this.tail=nn;
this.size++;
}
}
private int KNode(int k) {
Node slow = this.head;
Node fast = this.head;
for(int i=1;i<=k;i++) {
fast = fast.next;
}
while(fast != null) {
slow=slow.next;
fast=fast.next;
}
return slow.data;
}
}