Print in Reverse Hackerrank problem solution

Print in Reverse Hackerrank

You are given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty – in that case, do not print anything!

Input Format
You have to complete the void ReversePrint(Node* head) method which takes one argument – the head of the linked list. You should NOT read any input from stdin/console.

Output Format
Print the elements of the linked list in reverse order to stdout/console (using printf or cout) , one per line.

Sample Input

1 –> 2 –> NULL
2 –> 1 –> 4 –> 5 –> NULL

Sample Output

2
1
5
4
1
2
Explanation
1. First list is printed from tail to head hence 2,1
2. Similarly second list is also printed from tail to head.

Solution of Print in Reverse Hackerrank
/*
  Print elements of a linked list in reverse order as standard output
  head pointer could be NULL as well for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
void ReversePrint(Node *head)
{
    if(head == NULL) return;

    ReversePrint(head->next);
    cout<<head->data<<endl;
  // This is a "method-only" submission. 
  // You only need to complete this method. 
}
"""
 Print elements of a linked list in reverse order as standard output
 head could be None as well for empty list
 Node is defined as
 
 class Node(object):
 
   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

 
"""

def ReversePrint(head):

    if head is None:
        return
    if head.next is not None:
        ReversePrint(head.next)
    print head.data
  
  
  
  
/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     Node next;
  }
*/
    // This is a "method-only" submission. 
    // You only need to complete this method. 

void ReversePrint(Node head) {
  // This is a "method-only" submission. 
  // You only need to complete this method. 

    if(head==null)
        return;
    ReversePrint(head.next);
    
    System.out.println(head.data);
    
    return;
    
    
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.