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;

	reverse(root->link);
	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:-

    public void DisplayReverse2(Link<?> node){
    while(node.next!=null)
    DisplayReverse2(node.next);
    node.displayLink();
    }

  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.

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

    internalTempNode = internalNode -> next;

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

    Head->next = prev;
    prev = Head;
    prev->next = Head->next;
    Head = internalNode;

    if(internalNode == NULL){
    head = prev;
    }

    }

  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;
    }
    }

    • Milton on October 13, 2015 at 1:22 pm said:

      Nice and working code… Clarifies the following :

      1> How to create a link list
      2> How to create a node of a linked list
      3> Reverse the link 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:

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

  5. Ajay on June 7, 2015 at 11:15 pm said:

    private static void printReverse(Node head) {
    if(head==null)
    {
    return;
    }
    printReverse(head.getNext()) ;
    System.out.println(head.getData());

    }

Leave a Reply to Raghunathan Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation