Javascript Program To Check If Two Linked Lists Are Identical
Last Updated :
05 Sep, 2024
Improve
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.
// Node class to create a new node in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Linked List class
class LinkedList {
constructor() {
this.head = null;
}
// Method to add a new node at the end of the list
append(data) {
let newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
}
// Method to check if two linked lists are identical
areIdentical(listb) {
return areIdenticalRecur(this.head, listb.head);
}
}
// Recursive function to check if two linked lists are identical
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;
}
// Creating two linked lists
let list1 = new LinkedList();
let list2 = new LinkedList();
// Adding nodes to the first list
list1.append(1);
list1.append(2);
list1.append(3);
// Adding nodes to the second list
list2.append(1);
list2.append(2);
list2.append(3);
// Check if the two linked lists are identical
if (list1.areIdentical(list2)) {
console.log("The linked lists are identical.");
} else {
console.log("The linked lists are not identical.");
}
// Modifying the second list to make it different
list2.append(4);
// Check again after modifying the second list
if (list1.areIdentical(list2)) {
console.log("The linked lists are identical.");
} else {
console.log("The linked lists are not identical.");
}
// Node class to create a new node in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Linked List class
class LinkedList {
constructor() {
this.head = null;
}
// Method to add a new node at the end of the list
append(data) {
let newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
}
// Method to check if two linked lists are identical
areIdentical(listb) {
return areIdenticalRecur(this.head, listb.head);
}
}
// Recursive function to check if two linked lists are identical
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;
}
// Creating two linked lists
let list1 = new LinkedList();
let list2 = new LinkedList();
// Adding nodes to the first list
list1.append(1);
list1.append(2);
list1.append(3);
// Adding nodes to the second list
list2.append(1);
list2.append(2);
list2.append(3);
// Check if the two linked lists are identical
if (list1.areIdentical(list2)) {
console.log("The linked lists are identical.");
} else {
console.log("The linked lists are not identical.");
}
// Modifying the second list to make it different
list2.append(4);
// Check again after modifying the second list
if (list1.areIdentical(list2)) {
console.log("The linked lists are identical.");
} else {
console.log("The linked lists are not identical.");
}
Output
The linked lists are identical. The linked lists are not identical.
Complexity Analysis:
- 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!