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 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>
function areIdenticalRecur(a, b)
{
if (a == null && b == null )
return true ;
if (a != null && b != null )
return (a.data == b.data) &&
areIdenticalRecur(a.next, b.next);
return false ;
}
function areIdentical( listb)
{
return areIdenticalRecur( this .head,
listb.head);
}
</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!
Last Updated :
03 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...