Print the last k nodes of the linked list in reverse order

Given a linked list print out last kth nodes in reverse order. There can be many solution of this problem?. Here include common solution which is combination of iterative and recursive approach. Let as views an example to implement solution of this problem.

Suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Print last k nodes in reverse order

Here given list of code implementation process.

Above is simplest solution, We can solve this problem in following ways.

1) Reverse the linked list and display initials k element. And reverse the linked list again to get original state.

2) Use a collection (Array of size k) contains which is store last k elements. And print result in reverse order. This process used extra space.

3) Count length of linked list and store last k element into the stack. And print stack element.

There is standard solution you can solve this problem in many ways.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved