Friday, December 2, 2011

Nth node from end of a linked list


public static void Nth(LinkedList node, int k){
 
LinkedList curr = node ,temp = node;
if(node == null )
return;
while(--k!=0){

if(temp.next!=null)
 temp=temp.next;
else{
System.out.println("Less no of nodes");
   return;
}
}
while(temp.next!=null){
temp = temp.next;
curr=curr.next;
}
System.out.println(curr.value);
}

No comments:

Post a Comment