Java Program To Check If Two Linked Lists Are Identical Last Updated : 03 Aug, 2022 Comments Improve Suggest changes 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 Java Program To Check If Two Linked Lists Are Identical kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists +1 More Practice Tags : JavaLinked List Similar Reads Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the 8 min read Java Program For Union And Intersection Of Two Linked Lists Write a java program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:In 10 min read Java Program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3-> 3 min read How to Sort a LinkedList in Java? A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations.Sorting the nodes of a Singly Linked list in ascending order:Original ListSorted ListWe can sort the LinkedList by many sorting techniques:Selection SortInsertion sortQuick sortMerge sort Me 13 min read Java Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco 2 min read Java Program For Flattening A Linked List Given a linked list where every node represents a linked list and contains two pointers of its type: Pointer to next node in the main list (we call it 'right' pointer in the code below).Pointer to a linked list where this node is headed (we call it the 'down' pointer in the code below). All linked l 4 min read Java 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->6 Second linked list be 2- 5 min read How to Swap Two Elements in a LinkedList in Java? Given a Linked List, the task is to swap two elements without disturbing their links. There are multiple ways to swap. Elements can be swapped using by swapping the elements inside the nodes, and by swapping the complete nodes. Example: Input :- 10->11->12->13->14->15 element1 = 11 el 4 min read Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Like