-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlists-identical.cpp
76 lines (66 loc) · 1.61 KB
/
lists-identical.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
// http://www.geeksforgeeks.org/identical-linked-lists/
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
bool isEqual(Node *head1, Node *head2);
Node* addNode(Node *head, int data);
void printList(Node *head);
bool isEqual(Node *head1, Node *head2) {
while (true) {
if (head1 == NULL && head2 == NULL)
return true;
if (head1 == NULL && head2 != NULL)
return false;
if (head1 != NULL && head2 == NULL)
return false;
if (head1->data != head2->data)
return false;
head1 =head1->next;
head2 =head2->next;
}
}
Node* addNode(Node *head, int data) {
if (head == NULL)
return new Node(data);
Node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new Node(data);
return head;
}
void printList(Node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
Node *head1 = NULL;
head1 = addNode(head1, 1);
head1 = addNode(head1, 2);
head1 = addNode(head1, 3);
head1 = addNode(head1, 5);
Node *head2 = NULL;
head2 = addNode(head2, 1);
head2 = addNode(head2, 2);
head2 = addNode(head2, 3);
head2 = addNode(head2, 4);
cout<<"list 1: ";
printList(head1);
cout<<endl<<"list 2: ";
printList(head2);
cout<<endl<<"equality: ";
isEqual(head1, head2) ? cout<<"true" : cout<<"false";
cout<<endl;
return 0;
}