This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIdentical Linked Lists
89 lines (82 loc) · 1.73 KB
/
Identical Linked Lists
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
80
81
82
83
84
85
86
87
88
89
// { Driver Code Starts
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
bool areIdentical(struct Node *a, struct Node *b);
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<<"Identical"<<endl:cout<<"Not identical"<<endl;
}
return 0;
}
// } Driver Code Ends
/*
Structure of the node of the linked list is as
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
*/
// This function should return true if both
// linked lists are identical else return false.
bool areIdentical(struct Node *head1, struct Node *head2)
{
// Code here
int c1=0,c2=0;Node * t1=head1;Node * t2=head2;
while(t1!=NULL){
t1=t1->next;
c1++;
}
while(t2!=NULL){
t2=t2->next;
c2++;
}
if(c1!=c2){return 0;}
else{
while(head1!=NULL&&head2!=NULL){
if(head1->data != head2->data){return 0;}
head1=head1->next;
head2=head2->next;
}
return 1;
}
}