C++ Program To Check If Two Linked Lists Are Identical Last Updated : 10 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Iterative): To identify if two lists are identical, we need to traverse both lists simultaneously, and while traversing we need to compare data. C++ // An iterative C++ program to check if // two linked lists are identical or not #include<bits/stdc++.h> using namespace std; // Structure for a linked list node struct Node { int data; struct Node *next; }; /* Returns true if linked lists a and b are identical, otherwise false */ bool areIdentical(struct Node *a, struct Node *b) { while (a != NULL && b != NULL) { if (a->data != b->data) return false; /* If we reach here, then a and b are not NULL and their data is same, so move to next nodes in both lists */ a = a->next; b = b->next; } // If linked lists are identical, then // 'a' and 'b' must be NULL at this point. return (a == NULL && b == NULL); } /* UTILITY FUNCTIONS TO TEST fun1() and fun2() */ /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(struct Node** head_ref, int new_data) { // Allocate node struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); // Put in the data new_node->data = new_data; // Link the old list of the new node new_node->next = (*head_ref); // Move the head to point to the // new node (*head_ref) = new_node; } // Driver Code int main() { /* The constructed linked lists are : a: 3->2->1 b: 3->2->1 */ struct Node *a = NULL; struct Node *b = NULL; push(&a, 1); push(&a, 2); push(&a, 3); push(&b, 1); push(&b, 2); push(&b, 3); if(areIdentical(a, b)) cout << "Identical"; else cout << "Not identical"; return 0; } // This code is contributed by Akanksha Rai Output: Identical Time Complexity: O(n) where n is the length of the smaller list among a and b. Method 2 (Recursive): Recursive solution code is much cleaner than iterative code. You probably wouldn’t want to use the recursive version for production code, however, because it will use stack space which is proportional to the length of the lists. C++ // A recursive C++ function to check if two // linked lists are identical or not bool areIdentical(Node *a, Node *b) { // If both lists are empty if (a == NULL && b == NULL) return true; // If both lists are not empty, then // data of current nodes must match, // and same should be recursively true // for rest of the nodes. if (a != NULL && b != NULL) return (a->data == b->data) && areIdentical(a->next, b->next); // If we reach here, then one of the lists // is empty and other is not return false; } //This is code is contributed by rathbhupendra Time Complexity: O(n) for both iterative and recursive versions. n is the length of the smaller list among a and b. Auxiliary Space: O(n) for call stack because using recursion Please refer complete article on Identical Linked Lists for more details! Comment More infoAdvertise with us Next Article Menu driven program for all operations on singly linked list in C K kartik Follow Improve Article Tags : C++ Linked Lists Practice Tags : CPP Similar Reads Javascript Program To Check If Two Linked Lists Are Identical Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical.Method (Recursive): Recursive solution cod 3 min read How to Check if Two Vectors are Equal in C++? Two vectors are said to equal if all the elements at every index of vectors is equal. In this article, we will learn how to check if two vectors are equal or not in C++.The simplest method to check if two vector are equal is by using comparison operator (==). Letâs take a look at an example:C++#incl 3 min read Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the 8 min read Remove all occurrences of one Linked list in another Linked list Given two linked lists head1 and head2, the task is to remove all occurrences of head2 in head1 and return the head1. Examples: Input: head1 = 2 -> 3 -> 4 -> 5 -> 3 -> 4, head2 = 3 -> 4Output: 2 -> 5Explanation: After removing all occurrences of 3 -> 4 in head1 output is 2 - 9 min read Intersection point of two Linked List by marking visited nodes Given two linked lists of size N and M consisting of positive value nodes, having a common intersection point, the task is to find the intersection point of the two linked lists where they merge. Examples: Input: L1: 3 ? 6 ? 9 ? 15 ? 30, L2: 10 ? 15 ? 30Output: 15Explanation: From the above image, t 7 min read Print duplicates from a list of integers Given a Linked list of integers containing duplicate elements, the task is to print all the duplicates in the given Linked List. Examples: Input: list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]Output: [20, 30, -20, 60] Input: list = [5, 7, 5, 1, 7]Output: [5, 7] Naive Approach: To bas 11 min read Like