what is comparision of linklist?
when i read this question , arises two option
1.either compare value of each element.
2.just compare the length of both the link list.
pls help which one is right approach to solve the question.??

It depends on what you're looking for. The same question might be asked about string comparisons, or array comparisons, or binary search tree comparisons.

However, typically a comparison means comparing corresponding items. So you'd traverse both lists and compare the data in each node. When they don't match, you return the result of that comparison. If both lists match to the end, they're equal. If one list is shorter but the contents are otherwise equal, convention makes it "less than" the other list. The algorithm would be very similar to how you'd compare arrays or strings, just with pointer chasing rather than indexing:

struct node* a = list1->head;
struct node* b = list2->head;

// Check while both lists have valid nodes
while (a != NULL && b != NULL)
{
    // Check for a mismatch and return if there is one
    if (a->data != b->data)
    {
        return a->data < b->data ? -1 : +1;
    }

    // Move to the next node
    a = a->next;
    b = b->next;
}

// Check for complete equality (both lists were exhausted)
if (a == NULL && b == NULL)
{
    return 0;
}

// Use list length to determine the relation
return a == NULL ? -1 : +1;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.