Given a linked list, print reverse of it without modifying the list.
Input : 1 2 3 4 5 6 Output : 6 5 4 3 2 1 Input : 12 23 34 45 56 67 78 Output : 78 67 56 45 34 23 12
Given a Linked List, display the linked list in reverse without using recursion, stack or modifications to given list.
Input : 1->2->3->4->5->NULL Output :5->4->3->2->1->NULL Input :10->5->15->20->24->NULL Output :24->20->15->5->10->NULL
Below are different solutions that are now allowed here as we cannot use extra space and modify list.
1) Recursive solution to print reverse a linked list. Requires extra space.
2) Reverse linked list and then print. This requires modifications to original list.
3) A O(n2) solution to print reverse of linked list that first count nodes and then prints k-th node from end.
In this post, an efficient stack based solution is discussed.
1. First insert all the element in stack
2. Print stack till stack is not empty
Note: Instead of inserting data from each node into the stack, insert the node’s address onto the stack. This is because the size of the node’s data will be generally more than the size of the node’s address. Thus the stack would end up requiring more memory if it directly stored the data elements. Also, we cannot insert the node’s data onto the stack if each node contained more than one data member. Hence the simpler and efficient solution would be to simply insert the node’s address.
5 4 3 2 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to reverse a linked list using Stack
- Stack | Set 3 (Reverse a string using stack)
- Print reverse of a Linked List without actually reversing
- An interesting method to print reverse of a linked list
- Print reverse of a Linked List without extra space and modifications
- Print Doubly Linked list in Reverse Order
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Implement a stack using singly linked list
- Python | Stack using Doubly Linked List
- Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
- Reverse a Doubly linked list using recursion
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Sort a stack using a temporary stack
- Reverse a stack using recursion
- Reverse a number using stack
- Reverse a stack without using extra space in O(n)
- Reverse alternate levels of a perfect binary tree using Stack
- Reverse the Words of a String using Stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.