Kth element from last(cannot resolve one error lindly help me for the same.)

import java.util.*;
class LinkedList {
private class Node{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void addLast(int data){
Node nn = new Node();
nn.data = data;
nn.next = null;
if(size>0){
this.tail.next = nn;
}
if(size == 0){
this.head = nn;
this.tail = nn;
this.size=1;
}
else {
this.tail = nn;
this.size++;
}
}
public void getElementAt(int idx) throws Exception{
if(size == 0){
throw new Exception(“List is empty”);
}
if(idx<0 || idx>= size){
throw new Exception(“Invalid index”);
}
Node temp = this.head;
for(int i = 0;i<idx;i++){
temp = temp.next;
}
System.out.print(temp.data);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = 0;
LinkedList list = new LinkedList();
while(sc.nextInt() != -1){
list.addLast(sc.nextInt());
n++;
}
int k = sc.nextInt();
getElementAt(n-k+1);
}
}

u have to do this w/o calculating the size of the linkedlist

while(sc.nextInt() != -1){
list.addLast(sc.nextInt());
n++;
}

why u have written 2 time sc.nextInt()
actually, u are adding only the 2nd elem to the linkedlist and the 1st elem is used for comparing

hope this will help u
pls rate my work

appraoch w/o size:

suppose hame 3 rd last element print karana hai
so take to pntr curr n prev
to hum curr pntr ko 3 aage kar denge
now run the loop until curr nulll na ho jaaye

jab curr null hoga prev answer wale node pe hoga
becoz curr required no of nodes aage tha prev se

hope this will help u
pls rate my work

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.

// null pointer exception is showing please tell me the required correction in this.

/* null pointer exception is showing please tell me the required correction in this./ package com.codechef.javaapp; import java.util.; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); LinkedList l = new LinkedList(); int n1; while(true){ n1 = sc.nextInt(); if(n1 == -1){ break; } else{ l.addLast(n1); } } l.nFromLast(sc.nextInt()); } } class LinkedList{ private Node head; class Node{ int data; Node next; } private int size; private Node tail; public void nFromLast(int n){ this.nFromLast(this.head ,n); } private void nFromLast(Node node,int n){ int len = 0; Node temp; while(node !=null){ node = node.next; len++; } if(len<n){ return; } temp = node; for(int i = 0;i<len-n+1;i++){ temp = temp.next; } System.out.print(temp.data); } public void addLast(int data){ Node nn = new Node(); nn.data = data; nn.next = null; if(size>0){ this.tail.next = nn; } if(size == 0){ this.head = nn; this.tail = nn; this.size=1; } else { this.tail = nn; this.size++; } } }

hi @8006366388
first of all you have to do this without calculating the size of the LinkedList

@8006366388
here is the correction in your code
import java.util.*;

public class testing {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
LinkedList l = new LinkedList();
int n1;
while (true) {
n1 = sc.nextInt();
if (n1 == -1) {
break;
} else {
l.addLast(n1);
}
}
l.nFromLast(sc.nextInt());
}

public static class LinkedList {
private Node head;

class Node {
	int data;
	Node next;
}

private int size;
private Node tail;

public void nFromLast(int n) {
	this.nFromLast(this.head, n);
}

private void nFromLast(Node node, int n) {
	int len = 0;
	Node temp=node;
	while (node != null) {
		node = node.next;
		len++;
	}
	if (len < n) {
		return;
	}
	
	for (int i = 0; i < len - n ; i++) {
		temp = temp.next;
	}
	System.out.print(temp.data);
}

public void addLast(int data) {
	Node nn = new Node();
	nn.data = data;
	nn.next = null;
	if (size > 0) {
		this.tail.next = nn;
		this.tail = nn;
		this.size++;
	}
	else if (size == 0) {
		this.head = nn;
		this.tail = nn;
		this.size = 1;
	} 
		
	
}

}
}

things which i modified

  1. your add last function is not updating the tail and the size of the linked list.
    2.in your nfromLast function you assigning (temp=node) after node become null and then you are doing temp.next which is the cause of Null Pointer Exception

kashish sir what have u written under if condition in your code is showing error please do correct it

@8006366388
i have mailed you the submitted code.

@8006366388
please mark your doubt as resolved and rate my work

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.