Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reversing linked list

Tags:

People also ask

How do you reverse a linked list theory?

To reverse a LinkedList recursively we need to divide the LinkedList into two parts: head and remaining. Head points to the first element initially. Remaining points to the next element from the head. We traverse the LinkedList recursively until the second last element.

How hard is reversing a linked list?

Actually it is harder than that, but it isn't hard. We started with reverse a linked list and were told it was too easy. Since sorting can be done in ALMOST the same way as reversing, it seemed to be a reasonable step up. I've read that link and he doesn't have a problem with sorting/reversing linked list problems.

What is reverse order linked list?

In a singly linked list, order is determined by a given node's next property. This property can either reference another node or will point to null if this is the last node in the list. So reversing a linked list, simply means reassigning all the next properties, on every node.

Can you reverse a linked list?

The recursive approach to reverse a linked list is simple, just we have to divide the linked lists in two parts and i.e first node and the rest of the linked list, and then call the recursion for the other part by maintaining the connection.


I am trying to reverse a linked list using recursion and wrote the following code for it. The list is start of the list at the beginning.

 node *reverse_list_recursive(node *list)
 {
      node *parent = list;
      node *current = list->next;

      if(current == NULL)
       return parent;

      else
       {
           current = reverse_list_recursive(current);
           current->next = parent;
           printf("\n %d  %d \n",current->value,parent->value);
           return parent;
       }

  }

I could see that all the links are getting reversed. However when I try to display, I get an infinite prints of the numbers. I suspect an error when I am trying to reverse the link for the first number originally in the list.

What am I doing wrong?