Identical Linked Lists (original) (raw)

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:

As shown in figure linkedlists are not identical.

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

As shown in figure both are identical.

Try It Yourselfredirect icon

[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:

C++ `

// C++ program to check if two linked lists // are identical or not #include <bits/stdc++.h> using namespace std;

struct Node { int data; Node *next;

Node(int val)
{
    data = val;
    next = nullptr;
}

};

// Returns true if linked lists head1 and head2 are // identical, otherwise false bool areIdentical(Node *head1, Node *head2) { while (head1 != nullptr && head2 != nullptr) { if (head1->data != head2->data) return false;

    // Move to next nodes in both lists 
    head1 = head1->next;
    head2 = head2->next;
}

// If linked lists are identical,
// both 'head1' and 'head2' must be NULL
return (head1 == nullptr && head2 == nullptr);

}

int main() { // Create first linked list: 3 -> 2 -> 1 Node *head1 = new Node(3); head1->next = new Node(2); head1->next->next = new Node(1);

// Create second linked list: 3 -> 2 -> 1
Node *head2 = new Node(3);
head2->next = new Node(2);
head2->next->next = new Node(1);

// Function call
if (areIdentical(head1, head2))
    cout << "True";
else
    cout << "False";

return 0;

}

C

// C program to check if two linked lists // are identical or not #include <stdio.h>

struct Node { int data; struct Node* next; };

// Returns true if linked lists head1 and head2 are // identical, otherwise false int areIdentical(struct Node* head1, struct Node* head2) { while (head1 != NULL && head2 != NULL) { if (head1->data != head2->data) return 0;

    // Move to next nodes in both lists
    head1 = head1->next;
    head2 = head2->next;
}

// If linked lists are identical, 
  // both 'head1' and 'head2' must be NULL
return (head1 == NULL && head2 == NULL);

}

struct Node* createNode(int val) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = val; new_node->next = NULL; return new_node; }

int main() { // Create first linked list: 3 -> 2 -> 1 struct Node* head1 = createNode(3); head1->next = createNode(2); head1->next->next = createNode(1);

// Create second linked list: 3 -> 2 -> 1
struct Node* head2 = createNode(3);
head2->next = createNode(2);
head2->next->next = createNode(1);

// Function call
if (areIdentical(head1, head2))
    printf("True\n");
else
    printf("False\n");

return 0;

}

Java

// Java program to check if two linked lists // are identical or not

class Node { int data; Node next;

Node(int val)
{
    data = val;
    next = null;
}

}

class GfG {

// Returns true if linked lists head1 and head2 are
// identical, otherwise false
static boolean areIdentical(Node head1, Node head2)
{
    while (head1 != null && head2 != null) {
        if (head1.data != head2.data)
            return false;

        /* Move to next nodes in both lists */
        head1 = head1.next;
        head2 = head2.next;
    }

    // If linked lists are identical,
    // both 'head1' and 'head2' must be NULL
    return (head1 == null && head2 == null);
}

public static void main(String[] args)
{
    // Create first linked list: 3 -> 2 -> 1
    Node head1 = new Node(3);
    head1.next = new Node(2);
    head1.next.next = new Node(1);

    // Create second linked list: 3 -> 2 -> 1
    Node head2 = new Node(3);
    head2.next = new Node(2);
    head2.next.next = new Node(1);

    // Function call
    if (areIdentical(head1, head2))
        System.out.println("True");
    else
        System.out.println("False");
}

}

Python

Python program to check if two linked lists

are identical or not

class Node: def init(self, val): self.data = val self.next = None

Returns true if linked lists head1 and head2 are

identical, otherwise false

def areIdentical(head1, head2): while head1 is not None and head2 is not None: if head1.data != head2.data: return False

    # Move to next nodes in both lists
    head1 = head1.next
    head2 = head2.next

# If linked lists are identical,
# both 'head1' and 'head2' must be None
return head1 is None and head2 is None

if name == "main": # Create first linked list: 3 -> 2 -> 1 head1 = Node(3) head1.next = Node(2) head1.next.next = Node(1)

# Create second linked list: 3 -> 2 -> 1
head2 = Node(3)
head2.next = Node(2)
head2.next.next = Node(1)

# Function call
if areIdentical(head1, head2):
    print("True")
else:
    print("False")

C#

// C# program to check if two linked lists // are identical or not using System;

public class Node { public int Data; public Node Next;

public Node(int val)
{
    Data = val;
    Next = null;
}

}

class GfG {

// Returns true if linked lists head1 and head2 are
// identical, otherwise false
static bool AreIdentical(Node head1, Node head2)
{
    while (head1 != null && head2 != null) {
        if (head1.Data != head2.Data)
            return false;

        // Move to next nodes in both lists
        head1 = head1.Next;
        head2 = head2.Next;
    }

    // If linked lists are identical,
    // both 'head1' and 'head2' must be NULL
    return (head1 == null && head2 == null);
}

public static void Main(string[] args)
{
    // Create first linked list: 3 -> 2 -> 1
    Node head1 = new Node(3);
    head1.Next = new Node(2);
    head1.Next.Next = new Node(1);

    // Create second linked list: 3 -> 2 -> 1
    Node head2 = new Node(3);
    head2.Next = new Node(2);
    head2.Next.Next = new Node(1);

    if (AreIdentical(head1, head2))
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}

}

JavaScript

// JavaScript program to check if two linked lists // are identical or not

class Node { constructor(val) { this.data = val; this.next = null; } }

// Returns true if linked lists head1 and head2 are // identical, otherwise false function areIdentical(head1, head2) { while (head1 !== null && head2 !== null) { if (head1.data !== head2.data) { return false; }

    // Move to next nodes in both lists
    head1 = head1.next;
    head2 = head2.next;
}

// If linked lists are identical, 
// both 'head1' and 'head2' must be null
return head1 === null && head2 === null;

}

// Create first linked list: 3 -> 2 -> 1 let head1 = new Node(3); head1.next = new Node(2); head1.next.next = new Node(1);

// Create second linked list: 3 -> 2 -> 1 let head2 = new Node(3); head2.next = new Node(2); head2.next.next = new Node(1);

if (areIdentical(head1, head2)) { console.log("True"); } else { console.log("False"); }

`

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