c++ - reverse linked list using recursion -


i trying reverse linked list using recursion. made reverse() function reverse list. created linked list in main() , defined print() method. don't know mistake making. please me correct it. code snippets given below.

struct node {     int data;     struct node *next; }*head;  void reverse(node **firstnode,node *n) {     if(n==null)     {         head=n;         return;     }     reverse(&head,n->next);     struct node *q=n->next;     n->next=q;     q->next=null; }  void main() {     ......      head=first;     reverse(&first,first);     print(head); } 

it may not address question directly. however, mentioned c++11 in tags. so, take @ std::forward_list. standard container based on single linked-list.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -