fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class node{
  5. public:
  6. int data;
  7. node *next;
  8. //constructor
  9. node(int d){
  10. data=d;
  11. next=NULL;
  12. }
  13.  
  14. };
  15. void insertAtTail(node *&head, int data){
  16. if(head==NULL){
  17. head=new node(data);
  18. return;
  19. }
  20. node *tail=head;
  21. while(tail->next!=NULL){
  22. tail=tail->next;
  23. }
  24. tail->next=new node(data);
  25. return;
  26. }
  27. void buildInput(node *&head){
  28. int data;
  29. cin>>data;
  30. while(data!=-1){
  31. insertAtTail(head,data);
  32. cin>>data;
  33. }
  34. }
  35. void reverseIterative(node *&head){
  36. node * curr=head;
  37. node * n;
  38. node * prev =NULL;
  39. while(curr!=NULL){
  40. //store the next list
  41. n=curr->next;
  42. //make current node point to previous
  43. curr->next=prev;
  44. //update
  45. prev=curr;
  46. curr=n;
  47. }
  48. head = prev;
  49. }
  50.  
  51. node* reverseRecursive(node *head{
  52. if(head->next==NULL||head==NULL){
  53. return head;
  54. }
  55. node *smallHead= reverseRecursive(head->next);
  56. node * curr=head;
  57. curr->next->next=curr;
  58. curr->next=NULL;
  59. return smallHead;
  60. }
  61.  
  62. void print(node *head){
  63. //node *temp=head;
  64. while(head!=NULL){
  65. cout<<head->data;
  66. head=head->next;
  67. }
  68. }
  69. int main(){
  70. node *head=NULL;
  71. buildInput(head);
  72. reverseIterative(head);
  73. head = reverseRecursive(head);
  74. print(head);
  75. return 0;}
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:51:34: error: expected ‘)’ before ‘{’ token
 node* reverseRecursive(node *head{
                       ~          ^
                                  )
stdout
Standard output is empty