Thursday 14 February 2019

print the elements of linked list in reverse order and reversing single linked list

To print the elements of linked list in reverse order
void printLL(struct node * head)
{
    if(head == NULL)
        return;
    printLL(head->next);
    printf("%d->",head->data);
}

To print the elements of linked list in normal order

void printLL(struct node * head)
{
    if(head == NULL)
        return;
    printf("%d->",head->data);
    printLL(head->next);
  
}


Reversing a single linked list
void reverse()
{
    struct node *p,*c,*n;
    c=head
    p=NULL;
    while(c != NULL)
    {
        n=c->next; //stores address of next node
        c->next =p;
        p=c;
        c=n;
    }
    head=p;
} 

No comments:

Post a Comment