Find 'n' th node from end of Linked List (CODE) [updated]

preview_player
Показать описание
Find n th node from end of linked list. Write code for this.
Рекомендации по теме
Комментарии
Автор

correction -- while(f->next != NULL) not while(f->next == NULL)
return p not print p
Please make a note !

Alpine_Mist
Автор

I never usually you are such an amazing explainer i couldn't earned a subscriber

devkirandass
Автор

Thanks for the great explanation, the approach is really a smart one, was able to come up with my own solution after understanding the key intuition of fast and slow moving pointers

azeeztaiwo
Автор

I liked this video. Cuz it's was a important problem in linkedlist. But you should mathematically explain how it's working

sayaknaiya
Автор

Thank you sir..very nicely explained.

suchitranagarajan
Автор

Thank you so much. This is amazing content. Keep up the great work!

vincenr
Автор

Negative case not write right?? Bcz, suppose list is empty mean, ? And length of the node is not enough of n mean, how do n minus of one.??Instead of print we can return is also correct right???

messageconveyor
Автор

Sir I just need to ask if I revererse the linkedlist start for i=1, i<n is it possible?

bikramjitdas
Автор

thank you sir keep making more videos, please

ridimamittal
Автор

If we do this like 1st find length of the link list then find length-n +1 nth node of the list .this way we can also find nth node from end of the list

chandrasekharrout
Автор

osm bro....
really helpfulll
well done....
keep it up/

rajneeshpal
Автор

Don't you traverse the linked list almost twice? is that efficient?

MrVitaliq
Автор

Respected Sir, can you please make a video on topic ...pointers to structures...because i am confused that even though head, or ptr, or temp etc..they all are structure variables..and they all contain the ADDRESS to the memory,  named as the variable name..but still we need...*head, or *ptr, type variables..to store the address of the node....why..please explain???

DurgaShiva
Автор

But this code will fail if n is greater than the number of nodes in the linked list

ronaksengupta
Автор

it does'nt work for only first 2 nodes..

exquisiteronnie
Автор

Am I the only only one who watches Vivekanand's video in 1.25x speed? :)

anty_
Автор

it doesnt satisfy every condition. put n= 4 and then apply this you will get wrong answer

shraddhaagrahari
Автор

can I use f!=NULL instance of f->nexrt!=NULL;

uditasen
Автор

your code can not be compiled for multiple test cases
see my code
Node *Temp, *P;
Temp=P=head;
int len=0;
while(Temp!=NULL)
{
Temp=Temp->next;
len++;
}
if(n>len)
return -1;
Temp=head;
while(n!=0)
{
Temp=Temp->next;
n--;
}
while(Temp!=NULL)
{
Temp=Temp->next;
P=P->next;
}
return(P->data);
}

satyabratbiswal
Автор

you can actually do this without the use of a second while loop:


#written in Python

def getNode(head, positionFromTail):
#initialize two pointers
p = head
q = head
diff = 0

#we begin to increment the pointers at different times
#p will increment from the start as a sort of counter
#q will increment when diff >= positionFromTail
#when p reaches NULL, q will be at the "Nth" position from tail
#and we return q.data
while(p.next is not None):
p = p.next

if (diff >= positionFromTail):
q= q.next
else:
diff += 1

return q.data

RenatoRegalado