Function to find Identical LinkedList

Implementation in C:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node** ref, int newData)
{
    struct node* newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = newData;
    newNode->next = (*ref);
    (*ref) = newNode;
}
int identical(struct node* a,struct node* b)
{
    while(1)
    {
    if(a == NULL && b == NULL)
        return 1;
    if(a == NULL && b != NULL)
        return 0;
    if(a != NULL && b == NULL)
        return 0;
    if(a->data != b->data)
        return 0;
    a = a->next;
    b = b->next;
    }
}
int main()
{
    struct node* a = NULL;
    struct node* b = NULL;
    push(&a,1);
    push(&a,2);
    push(&a,3);
    push(&a,4);
    push(&b,1);
    push(&b,2);
    push(&b,3);
    push(&b,5);
    if(identical(a,b)==1)
        printf("Identical LinkedList");
    else
        printf("Not Identical");

}

Output:
Not Identical

Time Complexity:
O(n)

Comments

Popular posts from this blog

Three mislabeled Jar

Difference between Macro And function