×

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

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 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.

Strings in Data Structures

Strings and functions in C A string is a collection of characters. We'll learn how to declare strings, operate with strings in C programming, and use pre-defined string handling routines. We'll look...

7 minutes read.

Operations on 1D-Arrays

One Dimensional Array Operations Basic Methods The fundamental operations enabled by an array are listed below. Traverse prints each element of the array one by one.Insert a new element at the specified index.Delete...

8 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

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.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.