How to find nth last element in Singly Linked List


Consider the linked list

34 – 45 – 9 – 3 – 23 – 04

To find the 3rd last element from last:

Start pushing each element into stack.  So stack will contain:

Top of stack :-

04

23

3

9

45

34

End of stack

Now keep on popping the element from stack till the desired element is retrieved.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s