×

Identical Linked Lists

Identical Linked Lists

In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the same data, and the arrangement of data is also the same.

Example:

                             List1:2 -> 4 -> 6 -> 8 -> 10

                             List1:2 -> 4 -> 6 -> 8 -> 10

 Output:Identical

Method 1:(Iterative)

In this method, we will traverse both the given linked lists simultaneously, and while traversing, we will compare the data and arrangement of both the linked lists.

Algorithm:

 boolareIdentical(struct node *a, struct node *b)
 {
 while (a != NULL && b != NULL)
     {
 if (a -> info != b -> info)
 return false;
         a = a -> next;
         b = b -> next;
     }
 return (a == NULL && b == NULL);
 } 

An iterative C program to check if two linked lists are identical or not

 #include<stdio.h>
 #include<stdlib.h>
 #include<stdbool.h>
 struct node
 {
 int info;
 struct node * next;
 };
 struct node * start1 = NULL, * start2 = NULL;
 // For inserting the elements in the linked list
 void add(int item, struct node **temp)
 {
 struct node * t, * p;
 t = (struct node * )malloc( sizeof( struct node ));
 if(*temp == NULL)
 {
 * temp = t;
 (* temp) ->info = item;
 (* temp) ->next = NULL;
 return;
 }
 else
 {
 struct node * p = * temp;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // For checking if two linked lists are identical or not
 boolareIdentical(struct node *a, struct node *b)
 {
 while (a != NULL && b != NULL)
     {
 if (a -> info != b -> info)
 return false;
         a = a -> next;
         b = b -> next;
     }
 return (a == NULL && b == NULL);
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
 }
 while(t -> next != NULL)
 {
                         printf("%d -> ", t -> info);
                         t = t -> next;
 }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
 // Add element into the linked1
 add(1, &start1);
 add(10, &start1);
 add(3, &start1);
 add(4, &start1);
 add(9, &start1);
 add(8, &start1);
 // Add element into the linked2
 add(1, &start2);
 add(10, &start2);
 add(3, &start2);
 add(4, &start2);
 add(9, &start2);
 add(8, &start2);
 printf("list1: ");
 traverse(start1);
 printf("list2: ");
 traverse(start2);
 bool res = areIdentical(start1, start2);
 if(res == 1)
 printf("Linked lists are Identical");
 else
 printf("Linked lists are not Identical");
 return 0;
 } 

Output:

Identical Linked Lists

Method 2:(Recursive)

This method will work on a recursive solution to this problem because recursive code is much clearer than iterative code. Recursive code will use the stack space, which is proportional to the length of the linked lists.

Algorithm:

 boolareIdentical(struct Node *a, struct Node *b)
 {
     // If both lists are empty
 if (a == NULL && b == NULL)
 return true;
 if (a != NULL && b != NULL)
 return (a-> info == b -> info) &&
 areIdentical(a->next, b->next);
 return false;
 } 

An recursive C program to check if two linked lists are identical or not

 #include<stdio.h>
 #include<stdlib.h>
 #include<stdbool.h>
 struct node
 {
 int info;
 struct node * next;
 };
 struct node * start1 = NULL, * start2 = NULL;
 // For inserting the elements in the linked list
 void add(int item, struct node **temp)
 {
 struct node * t, * p;
 t = (struct node * )malloc( sizeof( struct node ));
 if(*temp == NULL)
 {
 * temp = t;
 (* temp) ->info = item;
 (* temp) ->next = NULL;
 return;
 }
 else
 {
 struct node * p = * temp;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // For checking if two linked lists are identical or not
 boolareIdentical(struct node *a, struct node *b)
 {
     // If both linked lists are empty
 if (a == NULL && b == NULL)
 return true;
 if (a != NULL && b != NULL)
 return (a -> info == b -> info) &&
 areIdentical(a -> next, b -> next);
 return false;
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
 }
 while(t -> next != NULL)
 {
                         printf("%d -> ", t -> info);
                         t = t -> next;
 }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
 // Add element into the linked1
 add(1, &start1);
 add(10, &start1);
 add(3, &start1);
 add(4, &start1);
 add(9, &start1);
 add(8, &start1);
 // Add element into the linked2
 add(1, &start2);
 add(10, &start2);
 add(3, &start2);
 add(4, &start2);
 add(9, &start2);
 add(7, &start2);
 printf("list1: ");
 traverse(start1);
 printf("list2: ");
 traverse(start2);
 bool res = areIdentical(start1, start2);
 if(res == 1)
 printf("Linked lists are Identical");
 else
 printf("Linked lists are not Identical");
 return 0;
 } 

Output:

Identical Linked Lists

Related Topics

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.