2. Write the code to reverse the linklist.
Non-Recursive
========
void reverse(struct node ** head)
{
struct node* prev = NULL;
struct node* current = *head;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
This trick might help you
there are three pointer ( prev, current, next)
next = current*
current* = prev
prev = current
current = next
if we see from above logic then before = is like top to bottom then bottom to top .
* marked pointer will have next.
Recursive
=======
struct node * recLinkedList(struct node *head, struct node *prev)
{
struct node *tmp = head->next;
head->next = prev;
if ( tmp != NULL)
recLinkedList(tmp, head);
return head
}
int main()
{
struct node *head = recLinkedList (head, NULL);
}
Ashish
22 Aug, 2016 3:22 PM
========
void reverse(struct node ** head)
{
struct node* prev = NULL;
struct node* current = *head;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
This trick might help you
there are three pointer ( prev, current, next)
next = current*
current* = prev
prev = current
current = next
if we see from above logic then before = is like top to bottom then bottom to top .
* marked pointer will have next.
Recursive
=======
struct node * recLinkedList(struct node *head, struct node *prev)
{
struct node *tmp = head->next;
head->next = prev;
if ( tmp != NULL)
recLinkedList(tmp, head);
return head
}
int main()
{
struct node *head = recLinkedList (head, NULL);
}
Report Error
Report Error
Please Login First Click Here