Open In App

Javascript Program To Check If Two Linked Lists Are Identical

Last Updated : 03 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
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.

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

Javascript




<script>
// A recursive javascript method to check if
// two linked lists are identical or not
function areIdenticalRecur(a,  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) &&
               areIdenticalRecur(a.next, b.next);
 
    // If we reach here, then one of the lists
    // is empty and other is not
    return false;
}
 
/* Returns true if linked lists a and b
   are identical, otherwise false */
function areIdentical( listb)
{
    return areIdenticalRecur(this.head,
                             listb.head);
}
// This code contributed by aashish1995
</script>


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!


Previous Article
Next Article

Similar Reads

C++ 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-&gt;2-&gt;3) and b(1-&gt;2-&gt;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
3 min read
C 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-&gt;2-&gt;3) and b(1-&gt;2-&gt;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
3 min read
Java 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-&gt;2-&gt;3) and b(1-&gt;2-&gt;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
3 min read
Python 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-&gt;2-&gt;3) and b(1-&gt;2-&gt;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
4 min read
Check if two Circular Linked Lists are identical
Given two circular linked lists L1 and L2, the task is to find if the two circular linked lists are identical or not. Note: Head of any linked list points to any node of the respective linked list and the lists can contain duplicate elements. Examples: Input: L1: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 1 -&gt; 2 -&gt; 6 L2: 5 -&gt; 1 -&gt; 2 -&gt;
13 min read
Identical Linked Lists
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. Write a function to check if the given two linked lists are identical. Examples: Input: a = 1-&gt;2-&gt;3-&gt;4, b = 1-&gt;2-&gt;3-&gt;4Output: Identical Input: a = 1-&gt;3-&gt;5-&gt;2, b = 1-&gt;3-&gt;4-&gt;6Output: Not Identical Recommended P
18 min read
Python List Equality | Program to check if two given matrices are identical
We are given two square matrices of same order. Check if two given matrices are identical. Examples: Input : A = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] B = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] Output: Matrices are identical We have existing solution for this problem please refer C Program to check if two give
3 min read
Program to check if two given matrices are identical
The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, a number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Implementation: C/C++ Code // C++
6 min read
C++ Program To Check if Two Matrices are Identical
The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, the number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal.  Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. C/C++ Code // C++ Program to c
2 min read
Javascript Program To Add Two Numbers Represented By Linked Lists- Set 1
Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1->4->0->5 // represents number 1405 Explanation: 563 + 842 = 1405
4 min read
Practice Tags :