VOOZH about

URL: https://www.geeksforgeeks.org/dsa/identical-linked-lists/

⇱ Identical Linked Lists - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Identical Linked Lists

Last Updated : 21 Aug, 2024

Given two singly linked lists. The task is to determine if the two linked lists are identical or not. Two linked lists are considered identical if they contain the same data in the same order. If the linked lists are identical, return true otherwise, return false.

Examples:

Input: LinkedList1: 1->2->3->4->5->6, LinkedList2: 99->59->42->20
Output: false
Explanation:

👁 Image

As shown in figure linkedlists are not identical.

Input: LinkedList1: 1->2->3->4->5, LinkedList2: 1->2->3->4->5
Output: true
Explanation:

👁 Image

As shown in figure both are identical.

[Expected Approach] By Traversal of Linked List - O(n) time and O(1) Space:

The idea is to traverse both the lists simultaneously and while traversing we need to compare data.

Code Implementation:


Output
True

Time Complexity: O(n), Here n is the length of the smaller list among two linked list.
Auxiliary Space: O(1).

Comment
Article Tags:
Article Tags: