Print a singly linked list in reverse order
Click for Solution
  • 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);
    }
    }
    }

  • A
    void 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;
    }
    }


  • A
    struct 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;
    }
    }
    


  • 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;
    }
    


[Insert Code]