Skip to content

Commit 696cdb5

Browse files
Create compare 2 linklist hackerrank
1 parent 6d52ebb commit 696cdb5

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

compare 2 linklist hackerrank

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class SinglyLinkedListNode {
6+
public:
7+
int data;
8+
SinglyLinkedListNode *next;
9+
10+
SinglyLinkedListNode(int node_data) {
11+
this->data = node_data;
12+
this->next = nullptr;
13+
}
14+
};
15+
16+
class SinglyLinkedList {
17+
public:
18+
SinglyLinkedListNode *head;
19+
SinglyLinkedListNode *tail;
20+
21+
SinglyLinkedList() {
22+
this->head = nullptr;
23+
this->tail = nullptr;
24+
}
25+
26+
void insert_node(int node_data) {
27+
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
28+
29+
if (!this->head) {
30+
this->head = node;
31+
} else {
32+
this->tail->next = node;
33+
}
34+
35+
this->tail = node;
36+
}
37+
};
38+
39+
void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
40+
while (node) {
41+
fout << node->data;
42+
43+
node = node->next;
44+
45+
if (node) {
46+
fout << sep;
47+
}
48+
}
49+
}
50+
51+
void free_singly_linked_list(SinglyLinkedListNode* node) {
52+
while (node) {
53+
SinglyLinkedListNode* temp = node;
54+
node = node->next;
55+
56+
free(temp);
57+
}
58+
}
59+
60+
// Complete the compare_lists function below.
61+
62+
/*
63+
* For your reference:
64+
*
65+
* SinglyLinkedListNode {
66+
* int data;
67+
* SinglyLinkedListNode* next;
68+
* };
69+
*
70+
*/
71+
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
72+
bool ans=false;
73+
if(head1!=NULL)
74+
{
75+
SinglyLinkedListNode *p=head1,*q=head2;
76+
while(p!=NULL)
77+
{
78+
if(q!=NULL)
79+
{
80+
if(p->data!=q->data)
81+
ans=false;
82+
else
83+
ans=true;
84+
p=p->next;
85+
q=q->next;
86+
}
87+
else
88+
return false;
89+
}
90+
return ans;
91+
}
92+
return ans;
93+
}
94+
95+
int main()
96+
{
97+
ofstream fout(getenv("OUTPUT_PATH"));
98+
99+
int tests;
100+
cin >> tests;
101+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
102+
103+
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
104+
SinglyLinkedList* llist1 = new SinglyLinkedList();
105+
106+
int llist1_count;
107+
cin >> llist1_count;
108+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
109+
110+
for (int i = 0; i < llist1_count; i++) {
111+
int llist1_item;
112+
cin >> llist1_item;
113+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
114+
115+
llist1->insert_node(llist1_item);
116+
}
117+
118+
SinglyLinkedList* llist2 = new SinglyLinkedList();
119+
120+
int llist2_count;
121+
cin >> llist2_count;
122+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
123+
124+
for (int i = 0; i < llist2_count; i++) {
125+
int llist2_item;
126+
cin >> llist2_item;
127+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
128+
129+
llist2->insert_node(llist2_item);
130+
}
131+
132+
bool result = compare_lists(llist1->head, llist2->head);
133+
134+
fout << result << "\n";
135+
}
136+
137+
fout.close();
138+
139+
return 0;
140+
}

0 commit comments

Comments
 (0)