check if one list is reverse of other

Given two linked list. We have to find that whether the data in one is reverse that of data in other. No extra space is to be used and traverse the lists only once

Comments

  1. int compare (struct node *list1, struct node *list2)
    {
    if (list2 == NULL)
    return 1;
    if (list1 == NULL)
    return 0;
    if (compare (list1, list2->next))
    {
    if ((list1)->data == list2->data)
    {
    (list1) = (list1)->next;
    return 1;
    }
    else return 0;
    }
    else
    return 0;
    }

    ReplyDelete
  2. In this solution stack space is used.
    however this function returns 1 even if list2 is NULL.
    so either check for its length before passing to compare function.

    ReplyDelete

Post a Comment

Popular posts from this blog