answersLogoWhite

0


Best Answer

In this case recursion is not necessary, an iterative process is more efficient.

Start by pointing at the head node. While this node has a next node, detach its next node and insert that node at the head. Repeat until the original head node has no next node. At that point the head has become the tail and all the nodes are completely reversed.

The following example shows how this can be implemented, where the list object contains a head node (which may be NULL), and each node has a next node. The tail node's next node is always NULL.

void reverse(list& lst)

{

if( node* p=lst.head )

{

while(p->next)

{

node* n=p.next; // point to the next node

p.next=n.next; // detach the next node

n.next=lst.head; // insert the detached node at the head

lst.head=n; // set the new head node

}

}

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you recursively reverse a singly linked list using c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.


What is the C plus plus programs to implement the stack ADT using a singly linked list?

#include<iostream> #include<time.h> #include<forward_list> int main() { // seed random number generator srand ((unsigned) time(NULL)); // a forward_list is a singly-linked list std::forward_list<int> stack; // push 10 integers onto stack for (int loop=0; loop<10; ++loop) { int num=rand(); std::cout<<"Pushing "<<num<<std::endl; stack.push_front (num); } // pop all integers while (!stack.empty()) { std::cout<<"Popping "<<stack.front()<<std::endl; stack.pop_front(); } }


What is the C plus plus coding for implementation of queue using circular linked list?

The implementation is the same as that for a singly-linked list, the only difference being that rather than maintaining separate pointers to both the head and tail, you only need to maintain a pointer to the tail, since the tail's next node is always the head, which will be itself if there is only one node. As with all queues, insertions occur at the tail and extractions occur at the head.


What are the operations take more time in the implementation of list using singly linked list?

Random access is not possible in constant time; it must be done in linear time. This means insertions and extractions are amortized operations unless you currently hold a pointer or reference to the node to be extracted or the node preceding the insertion point, such as the tail node.

Related questions

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.


Evaluate the polynomial expression using linked list using c?

Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.


What is a singly linked linear list?

A singly linked list is a linked list which only provides links in "one direction". Using a metaphor, a singly linked list is a one way street, while a doubly linked list is a two way street. Once you move forward in a singly linked list, there is no way to go backwards unless you kept your reference/pointer from before. A singly linked list would look like this: start ----> node1---->node2---->node3 ----> NULL You will see that node2 only has a link forward to node3 - it does not have a link backwards to node1, even though node1 has a link forwards to node2. To prevent us from permanently losing access to portions of the linked list, we generally keep a reference/pointer to "start". A doubly linked list would have twice the number of pointers/references as a singly linked list - making it very inefficient to store small datatypes. On the other hand, it would be possible to move both forwards and backwards with a doubly linked list because you have links pointing both forwards and backwards.


Using recursion reverse a singly linked list?

Let's suppose your list consists of elements that have an integer part (the data) and a reference (or pointer) to the next list element.Now your function would look like this:void Reverse(listitem){if(listitem.next exists) //you have to check this in a way depending on your implementationReverse(listitem.next); //the function goes right onto the next elementprint(listitem.data); //or do anything that is needed}In a nutshell, this function first explores the whole linked list, and when it reaches the end, the individual function calls reach the data processing block, and then terminate, thus giving the control back to the previous listelement's function call.


How do you implement a doubly linked list by using singly linked list?

To implement a doubly linked list using a singly linked list, you can create two nodes in each element of the singly linked list - one for the next element and another for the previous element. This way, each node will have access to both its previous and next nodes, effectively creating a doubly linked list structure using a singly linked list implementation.


How do you make sentences using 'strongly linked'?

Obesity has been strongly linked to diabetes.


What is the C plus plus programs to implement the stack ADT using a singly linked list?

#include<iostream> #include<time.h> #include<forward_list> int main() { // seed random number generator srand ((unsigned) time(NULL)); // a forward_list is a singly-linked list std::forward_list<int> stack; // push 10 integers onto stack for (int loop=0; loop<10; ++loop) { int num=rand(); std::cout<<"Pushing "<<num<<std::endl; stack.push_front (num); } // pop all integers while (!stack.empty()) { std::cout<<"Popping "<<stack.front()<<std::endl; stack.pop_front(); } }


Can a linked list implemented using an array be accessed by giving the location?

A linked list implemented with an array defeats the purpose of using a linked list, which is to address the memory allocation problems associated with arrays.


What is the C plus plus coding for implementation of queue using circular linked list?

The implementation is the same as that for a singly-linked list, the only difference being that rather than maintaining separate pointers to both the head and tail, you only need to maintain a pointer to the tail, since the tail's next node is always the head, which will be itself if there is only one node. As with all queues, insertions occur at the tail and extractions occur at the head.


What are the operations take more time in the implementation of list using singly linked list?

Random access is not possible in constant time; it must be done in linear time. This means insertions and extractions are amortized operations unless you currently hold a pointer or reference to the node to be extracted or the node preceding the insertion point, such as the tail node.


Disadvantages of circular link list as compared to singly link list?

As compared with doubly-linked lists, one disadvantage is that singly-linked lists can only be efficiently traversed in one direction. Finding the (n - 1)th element, given only a pointer to the nth, requires scanning the list from the first element up to the (n - 1)th. Thus (if memory is too limited to permit tricks such as creating a reversed list) to iterate over the entire list in reverse order would require n + (n - 1) + ... + 1 = n(n + 1)/2 link traversals, which grows quadratically with n. Obviously for a doubly-linked list the time merely grows linearly with n.A down-side of all linked lists versus arrays is that random access can be inefficient; the time taken to find an element with a randomly chosen index grows in direct proportion to the list's length (since we must scan from one of the ends). In contrast, an array allows us to index directly to the elements with simple pointer arithmetic, in a time independent of the array's size - at least in an idealised environment.