-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidentical_linked_list.cpp
79 lines (69 loc) · 1.85 KB
/
identical_linked_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 29th June 2024
// Identical Linked List
/*
Given the two singly Linked Lists respectively. The task is to check whether two linked lists are identical or not.
Two Linked Lists are identical when they have the same data and with the same arrangement too. If both Linked Lists are identical then return true otherwise return false.
Example1:
Input:
LinkedList1: 1->2->3->4->5->6
LinkedList2: 99->59->42->20
Output:
false
Example2:
Input:
LinkedList1: 1->2->3->4->5
LinkedList2: 1->2->3->4->5
Output:
true
*/
#include <bits/stdc++.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
bool areIdentical(struct Node *head1, struct Node *head2);
int main() {
int T;
cin >> T;
while (T--) {
int n1, n2, tmp, d1, d2;
struct Node *head1 = NULL, *tail1 = NULL;
struct Node *head2 = NULL, *tail2 = NULL;
cin >> n1;
cin >> d1;
head1 = new Node(d1);
tail1 = head1;
while (n1-- > 1) {
cin >> tmp;
tail1->next = new Node(tmp);
tail1 = tail1->next;
}
cin >> n2;
cin >> d2;
head2 = new Node(d2);
tail2 = head2;
while (n2-- > 1) {
cin >> tmp;
tail2->next = new Node(tmp);
tail2 = tail2->next;
}
areIdentical(head1, head2) ? cout << "true" << endl : cout << "false" << endl;
}
return 0;
}
// Function to check whether two linked lists are identical or not.
bool areIdentical(struct Node *head1, struct Node *head2) {
if(!head1 && !head2)
return true;
if(!head1 || !head2 || head1->data != head2->data)
return false;
return areIdentical(head1->next, head2->next);
}