get data at given index

PHOTO EMBED

Thu Sep 09 2021 06:33:14 GMT+0000 (Coordinated Universal Time)

Saved by @prashantkr006 #java

int getAt(int idx) throws Exception {
		  
		  if(size == 0){
		    throw new Exception("LinkedList is empty");
		  }
		  if(idx < 0 || idx >= this.size){
		    throw new Exception("Invalid index");
		  }
		  
		  Node temp = this.head;
		  for(int i=1;  i <= idx; i++){
		    temp = temp.next;
		  }
		  
		  return temp.data;
		  
		}
content_copyCOPY