Print a singly linked list in reverse order Click for Solution |
-
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
A void reverse(Node *root)
{
if(root==NULL)
printf("list is empty");
else
{
while(root->next!=NULL)
{
root=root->next;
reverse(Node *root);
printf("%d\t",root->data);
}
}
}
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
Avoid reverse(Node *root) { Node *y=NULL; if(root==NULL) printf("The list is empty"); else { Node *a; whiel(root!=NULL) { a=root; root=root->next; a->next=y; y=a; } }
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
Astruct node { int val; struct node *next; }*ptr1,*ptr2,*start1=NULL,*start2=NULL,*newnode; void reverse() { ptr1=start1->link;//assuming start1 points to the existing linked list newnode=(struct node *)malloc(sizeof(struct node)); newnode->link = null; newnode = start1; start2 = newnode;//Storing the first element in another list, whose starting is start2 while(ptr1->link!=NULL) { newnode=(struct node *)malloc(sizeof(struct node)); newnode->link = null; newnode = ptr1;/*Insert the element pointed by the ptr1 to the beginning of the new linked list*/ newnode->link=start2; start2 = newnode; } ptr2=start2; while(ptr2->link!=NULL) { printf("%d\t",ptr2->value); ptr2=ptr2->link; } }
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
A use recursion to solve this<#include stdio .h> struct node { int val; struct node *next; } void printlist(struct node *a) { if (a->next!=NULL) printlist(a->next); printf("%d",a->value); return; }
Explore
- GPL4you
- Home
- Prepdoor - Online Mock Test
- About
- FAQs
- Contact
- Contact Us
© 2011 - gpl4you | All Rights Reserved.