forked from priyanka090700/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentical_linked_list.cpp
More file actions
93 lines (81 loc) · 1.66 KB
/
Identical_linked_list.cpp
File metadata and controls
93 lines (81 loc) · 1.66 KB
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
90
91
92
93
/*
Input:
LinkedList1: 1->2->3->4->5->6
LinkedList2: 99->59->42->20
Output: Not identical
*/
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
struct Node
{
int data;
struct Node* next;
};
// } Driver Code Ends
//User function Template for C
//Function to check if 2 linked lists are identical
bool areIdentical(struct Node *head1, struct Node *head2)
{
struct Node *start1 = head1;
struct Node *start2 = head2;
while(start1 && start2 != NULL)
{
if(start1->data != start2->data)
return false;
if(start1->data == start2->data)
{
start1 = start1->next;
start2 = start2->next;
}
}
return true;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
int first;
scanf("%d", &first);
struct Node *head1;
head1 = (struct Node *) malloc(sizeof(struct Node));
head1->data = first;
head1->next = NULL;
struct Node *tail = head1;
for (int i = 1; i < n; ++i)
{
int data;
scanf("%d", &data);
struct Node *temp;
temp = (struct Node *) malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
tail->next = temp;
tail = tail->next;
}
scanf("%d", &n);
scanf("%d", &first);
struct Node *head2;
head2 = (struct Node *) malloc(sizeof(struct Node));
head2->data = first;
head2->next = NULL;
tail = head2;
for (int i = 1; i < n; ++i)
{
int data;
scanf("%d", &data);
struct Node *temp;
temp = (struct Node *) malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
tail->next = temp;
tail = tail->next;
}
areIdentical(head1, head2) ? printf("Identical\n") : printf("Not identical\n");
}
return 0;
}