Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
suppose
12 34 56 87 99 34
i am going to print 87
can you provide any efficient logic in linked list
Posted
Comments
Sergey Alexandrovich Kryukov 2-Oct-14 1:33am    
puts("87");
—SA

As your code walks down the list, save the three most recent values. When your code reaches the end of the list, print the oldest of the three saved values.
 
Share this answer
 
Comments
CPallini 2-Oct-14 3:55am    
5
If it's a singly linked list, the method mentioned in solution 1 is appropriate.

If it's a doubly linked list, you have in addition to that the option to go to the end of the list and then go two elements backwards. For long lists this will be much faster than to traverse the whole list.
 
Share this answer
 
Comments
CPallini 2-Oct-14 3:55am    
5
nv3 2-Oct-14 4:39am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900