# Print Linked List Elements in Reverse order

Write a program to print elements of a linked list in reverse order by using same single linked list.

Solution: We can solve this problem without actually reversing the linked list. Using recursion we have given the below solution. This implementation will be internally using stack to store each recursive function call.

```void reverse(node *root)
{
if(!root)
return;

cout << root->data << endl;
}
```

Complexity: O(N), where n is nodes in linked list.

### 0 Thoughts on “Print Linked List Elements in Reverse order”

1. Anonymous on November 1, 2014 at 11:48 am said:

Please tell me what is the problem with this piece of code:-

while(node.next!=null)
DisplayReverse2(node.next);
}

2. Raghunathan on February 17, 2015 at 2:05 pm said:

This is a straightforward implementation, No recursion used. Check it , Kindly do let me know your feedback.

Complexity : O(n).

Considering internalNode,internalTempNode is internal node. release() is internal function , as name states it will release the internal temp node.

if(internalNode == NULL){
internalNode = Head – > next;
}
else{
internalNode = internalTempNode;
}

internalTempNode = internalNode -> next;

if(internalTempNode == NULL){
release(temp1);
}

if(internalNode == NULL){
}

}

3. Neo Seeker on February 26, 2015 at 8:36 am said:

Here is a simple Java implementation:

` public class ReverseLinkedList { MyLinkedList head; public static void main (String arg[]) { ReverseLinkedList myList = new ReverseLinkedList(); for(int i = 1; i < 10; i++) { myList.createLinkedList(new Integer(i).toString()); } //myList.printLinkedList(); System.out.println("Reverse: "); myList.printReverseLinkedList(myList.head); } void createLinkedList(String data) { if(head == null || head.getData() == null || head.getData().isEmpty()) { head = new MyLinkedList(data, null); } else { MyLinkedList temp = head; while(temp.getNext() != null) { temp = temp.getNext(); } MyLinkedList node = new MyLinkedList(data, null); temp.setNext(node); } } /*void printLinkedList() { MyLinkedList temp = head; while(temp != null) { System.out.print(temp.getData() + " "); temp = temp.getNext(); } }*/ void printReverseLinkedList(MyLinkedList node) { if(node != null) { printReverseLinkedList(node.getNext()); System.out.print(" " + node.getData()); } } } class MyLinkedList { private String data; private MyLinkedList next; MyLinkedList(String data, MyLinkedList next) { this.data = data; this.next = next; } public String getData() { return data; } public MyLinkedList getNext () { return next; } public void setData(String data) { this.data = data; } public void setNext (MyLinkedList next) { this.next = next; } } `

• Nice and working code… Clarifies the following :

1> How to create a link list
2> How to create a node of a linked list

I have executed the code and it works well. Thanks for the code.

4. Azra Irshad Rabbani on May 5, 2015 at 2:14 am said:

Should be simpler as follows:

if(node == null) { // base case
return;
}
printLinkedListInReverseRecursive(node.next); //making progress towards base case
System.out.print(node.data+ “, “);
}

5. private static void printReverse(Node head) {