my code is compiled but it is giving wrong output despite having the correct logic please tell my mistake where I am wrong
Finding kth node from last of Linked list
public static void main(String args[]) {
Main mm=new Main();
mm.addLast(1);
mm.addLast(2);
mm.addLast(3);
mm.addLast(4);
mm.addLast(5);
mm.addLast(6);
mm.kthNode(3);
}
Why are you doing hardcode
take input from Scanner
public void kthNode(int k){
Node first=this.head;
Node sec=this.head;
int count=1;
while(count<=k){
sec=sec.next;
count++;
}
while(sec!=null){
first=first.next;
sec=sec.next;
}
System.out.println(first.data);
}