answersLogoWhite

0


Best Answer

Reversing a singly-linked list is (surprisingly) simple and can be done non-recursively. The following function accepts a reference to the current head of the list and returns the new head after reversing the list.

node* reverse (node* head) {

if (!head) return head;

node *lead, *temp;

lead = head;

while (lead->next) {

temp = lead->next;

lead->next = temp->next;

temp->next = head;

head = temp;

}

return head;

}

Doing the same thing recursively is a bit more tricky but it can be done as follows.

node* reverse (node* head, node* prev=NULL) {

node* temp;

if (!head) {

return head;

} else if (!head->next) {

head->next = prev;

return head;

} else {

temp = head->next;

head->next = prev;

return reverse (temp, head);

}

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you reverse a single linked list in recursive method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is macro a recursive function?

If you're asking if the c preprocessor supports recursive macros, the answer is no. The preprocessor is single-pass and since the "function" must be defined before it can be referenced, it can not be recursive.


When a single entity is related to itself then the relationship is termed as?

It will be a recursive relationship.


Which is the easy insertion operator single linked-list or double-linked list?

It is easier to insert into a singly linked list.


Which operation is perform more efficiently by doubly linked list than by single linked list?

zsd


Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


Which text compression technique replaces a frequently used word with a single character?

One fairly straightforward method of text compression is called keyword encoding, in which frequently used words are replaced with a single character. To decompress the document, you reverse the process: replace the single characters with the appropriate full word.


What is the relationship between monomers andpolymers?

Monomers are single units while polymers are monomers linked together. So with polysaccharides being polymers or monomers linked together, then think of a single monomer of sugar such as maltose. When you link a bunch of maltose together then they form a polysaccharide polymer of sugars. Glycogen is an example of a polymer of sugars. Glycogen is the body method of sugar storage in your liver and muscle tissue....


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


Method of marching in tight formation?

Lockstep-single file with the right hand on the shoulder of the man in front. Mainly in US prisons, especially chain gangs where the ankles were linked by a short chain.


What method of representation toposheet?

individual practical method with single toposheet


When single sugars are linked together into long chains the result is a?

Polysaccharide