Reverse a doubly linked list recursively using C++ This is what I have. What am
ID: 3719826 • Letter: R
Question
Reverse a doubly linked list recursively using C++
This is what I have. What am I doing wrong?
37 int reverse_list(node*head)
38 {
39 if(!head)
40 return 0;
41
42 node* temp = head->next;
43 head -> next = head -> previous;
44 head -> previous = temp;
45
46 if(!head->previous)
47 return 0;
48
49 return reverse_list(head-> previous);
50 }
Explanation / Answer
node * reverse_list(node* head) { if(head->next == NULL) { head->next = head->prev; return head; } else { node* temp = head->next; head->next = head->previous; head->previous = temp; return reverse_list(temp); } } // this is how you would call it node *new_root = reverse_list(root);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.