InterviewSolution
| 1. |
Write A Program To Print A Linked List In Reverse Order? E.g. Print Linked List From Tail To Head? |
|
Answer» You can print nodes of linked list in REVERSE order by using Stack data structure in two steps: Step 1: Traverse the linked list from the HEAD and put the VALUE of each node into Stack until you reach the LAST node. This will take O(n) time. Step 2: Pop the elements out from the stack and print. This will take O(1) time. Input: 1->2->3 Output: 3 2 1 You can print nodes of linked list in reverse order by using Stack data structure in two steps: Step 1: Traverse the linked list from the head and put the value of each node into Stack until you reach the last node. This will take O(n) time. Step 2: Pop the elements out from the stack and print. This will take O(1) time. Input: 1->2->3 Output: 3 2 1 |
|