+ 3
How to reverse the input
input contains elements of a linked list seperated by a space and terminated by -1 output reverse linked list how to do it
5 Respostas
+ 4
/* C++ snippet . Note I assume that you have implemented your node using structures */
struct Node
{
int data;
struct Node* next;
};
static void reverse(struct Node** head)
{
struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
/*Inside main */
...
struct Node* head = NULL;
...
//Push values to linked list
...
reverse(&head);
...
/* The idea is you set Prev to null. Set current to next and current's next to prev in loop */
+ 3
in c++ there's method called reverse in #include <algorithm>
just use this one line code:
reverse(myData.begin(),myData.end());
+ 2
Is this a challenge from dcoder??
+ 1
thanks but without functions will be better like using loop
+ 1
yes