Skip to content

Commit 8230dfe

Browse files
authored
Merge pull request #758 from ABHIT33/main
Identical Linked Lists.cpp
2 parents 2b4c687 + 4ffad0c commit 8230dfe

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<stdbool.h>
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
struct Node {
8+
int data;
9+
struct Node *next;
10+
Node(int x) {
11+
data = x;
12+
next = NULL;
13+
}
14+
};
15+
16+
bool areIdentical(struct Node *a, struct Node *b);
17+
18+
19+
int main()
20+
{
21+
int T;
22+
cin>>T;
23+
while(T--){
24+
int n1, n2, tmp , d1 , d2;
25+
struct Node *head1 = NULL , *tail1=NULL;
26+
struct Node *head2 = NULL , *tail2 =NULL;
27+
cin>>n1;
28+
cin>>d1;
29+
head1 = new Node(d1);
30+
tail1 = head1;
31+
while(n1-- > 1){
32+
cin>>tmp;
33+
tail1->next = new Node(tmp);
34+
tail1 = tail1->next;
35+
}
36+
cin>>n2;
37+
cin>>d2;
38+
head2 = new Node(d2);
39+
tail2 = head2;
40+
while(n2-- >1)
41+
{
42+
cin>>tmp;
43+
tail2->next = new Node(tmp);
44+
tail2 = tail2->next;
45+
}
46+
areIdentical(head1, head2)?cout<<"Identical"<<endl:cout<<"Not identical"<<endl;
47+
}
48+
return 0;
49+
}
50+
// } Driver Code Ends
51+
52+
53+
/*
54+
Structure of the node of the linked list is as
55+
struct Node {
56+
int data;
57+
struct Node *next;
58+
Node(int x) {
59+
data = x;
60+
next = NULL;
61+
}
62+
};
63+
*/
64+
65+
// This function should return true if both
66+
// linked lists are identical else return false.
67+
bool areIdentical(struct Node *head1, struct Node *head2)
68+
{
69+
// Code here
70+
Node *cur1=head1 ,*cur2=head2;
71+
if(cur1==NULL && cur2==NULL)
72+
return 1;
73+
if(cur1==NULL || cur2==NULL)
74+
return 0;
75+
while(cur1!=NULL && cur2!=NULL)
76+
{
77+
if(cur1->data != cur2->data)
78+
return 0;
79+
80+
cur1=cur1->next;
81+
cur2=cur2->next;
82+
}
83+
return 1;
84+
85+
}

0 commit comments

Comments
 (0)