Write a recursive function to print portion of a linked list


Write a recursive function to print portion of a linked list in reverse order. The portion is to be printed between node P and Q.

Solution

Hi, I wrote function in C++.

// function to print linked list in reverse order from node P to node Q

// lets assume that structure of Node is:
struct Node{

   Node *next;
   int data;
};

void printRecursive(Ndoe *p, Node *q){

   // if list is empty
   if(p == NULL)
       return;
  
   // base case, if we have only one node
   if(p == q){
       cout<<q->data<<endl;
       return;
   }

   // call recursively for next node
   printRecursive(p->next);

   // print p\'s data
   cout<<p->data<<endl;

}

 Write a recursive function to print portion of a linked list in reverse order. The portion is to be printed between node P and Q. SolutionHi, I wrote function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site