Java Program To Check If Two Linked Lists Are Identical Last Updated : 03 Aug, 2022 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. Java // An iterative Java program to check if two // linked lists are identical or not class LinkedList { // Head of list Node head; // Linked list Node class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Returns true if linked lists a and b are identical, otherwise false */ boolean areIdentical(LinkedList listb) { Node a = this.head, b = listb.head; 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(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); // 3. Make next of new Node as head new_node.next = head; // 4. Move the head to point to new Node head = new_node; } // Driver code public static void main(String args[]) { LinkedList llist1 = new LinkedList(); LinkedList llist2 = new LinkedList(); /* The constructed linked lists are : llist1: 3->2->1 llist2: 3->2->1 */ llist1.push(1); llist1.push(2); llist1.push(3); llist2.push(1); llist2.push(2); llist2.push(3); if (llist1.areIdentical(llist2) == true) System.out.println("Identical "); else System.out.println("Not identical "); } } // This code is contributed by Rajat Mishra Output: Identical 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. Java // A recursive Java method to check if two // linked lists are identical or not boolean areIdenticalRecur(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) && 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 */ boolean areIdentical(LinkedList listb) { return areIdenticalRecur(this.head, listb.head); } 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 Javascript Program To Check If Two Linked Lists Are Identical K kartik Follow Improve Article Tags : Java Linked Lists Practice Tags : Java 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 Javascript Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6Second linked list be 2- 3 min read Javascript Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read LinkedList addAll() Method in Java In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an 3 min read Javascript Program For Removing Duplicates From An Unsorted Linked List Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -> 5 min read LinkedList contains() Method in Java In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list.Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The p 2 min read Like