Skip to main content

Nth Node from End in LinkedList

Nth Node from End in LinkedList
1) Find length of  linked list.
2) Traverse (length-position+1) element from begin.


 //nth Node from end
 public Node nodeFromEnd(Node head,int position){
  Node temp=head;
  int length=1;
  while(temp.next!=null)
  {
   length++;
   temp=temp.next;
  }
  System.out.println("length"+length);
  temp=head;
  int c=1;
  while(c!=(length-position+1))
  {
   temp=temp.next;
   c++;
  }
  return temp;
 }
 

Comments

  1. SuperSlots is model new} on-line casino web site that welcomes its gamers with a generous 6-tier bonus package price as 카지노 much as} $6,000. Online sports betting, casino gaming, and poker are legal in Michigan. Under state laws, the industry is regulated by the Michigan Gaming Control Board . This regulatory body sets the rules and ensures they're upheld.

    ReplyDelete

Post a Comment

.

Popular posts from this blog

Breadth First Search Implementation in Java

Breadth First Search Implementation in Java For Graph Class Design and DFS implementation please refer to below link:- depth-first-search-implementation-in-java /* 1. Pick a vertex. 2. Get Adjacent Vertex. 3. Insert in queue ( at tail) 4. Remove vertex from queue( head) 5. Do until no vertex left in a queue. */ void searchBFS () { //this is how we define queue using LinkedList Queue < Character > queue = new LinkedList < Character >(); queue . add ( vertexlist [ 0 ]. label ); while (! queue . isEmpty ()) { //element at head (Function in main graph class please use above link) ArrayList adjvertex = getNeighbours ( queue . peek ()); System . out . println ( queue . peek ()); //element removed at head queue . remove (); if ( adjvertex != null ) { for ( int i = 0 ; i < adjvertex . size (); i ++) { //element added at tail queue . add