-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-structures-problems-hr.txt
18466 lines (15297 loc) · 414 KB
/
data-structures-problems-hr.txt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------
DATA-STRUCTURES.1
easy
----------
PROBLEM STATEMENT:
An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, [expression]).
Given an array, [expression] integers, print each element in reverse order as a single line of space-separated integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
----------
TOP SOLUTION:
----------
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N;
cin>>N;
vector<int> arr(N);
for (int i=0;i<N;i++)
cin>>arr[i];
reverse(arr.begin(),arr.end());
for (int i=0;i<N;i++)
cout<<arr[i]<<" ";
return 0;
}
----------
====================
----------
DATA-STRUCTURES.2
easy
----------
PROBLEM STATEMENT:
Given a [expression]:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in [expression]'s graphical representation:
a b c
d
e f g
There are [expression], then print the maximum hourglass sum.
For example, given the 2D array:
-9 -9 -9 1 1 1
0 -9 0 4 3 2
-9 -9 -9 1 2 3
0 0 8 6 6 0
0 0 0 -2 0 0
0 0 1 2 4 0
We calculate the following [expression] hourglass values:
-63, -34, -9, 12,
-10, 0, 28, 23,
-27, -11, -2, 10,
9, 17, 25, 18
Our highest hourglass value is [expression] from the hourglass:
0 4 3
1
8 6 6
Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.
Function Description
Complete the function hourglassSum in the editor below. It should return an integer, the maximum hourglass sum in the array.
hourglassSum has the following parameter(s):
arr: an array of integers
----------
TOP SOLUTION:
----------
#include <cmath>
#include <cstdio>
#include <vector>
#include <climits>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[6][6],s;
int m=INT_MIN;
//int s;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
cin>>a[i][j];
}
}
// int j;
for(int i=0;i<4;i++)
{
// j=0;
for(int j=0;j<4;j++)
{
s=(a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2]);
if(s>m)
m=s;
}
}
cout<<m;
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
----------
====================
----------
DATA-STRUCTURES.3
easy
----------
PROBLEM STATEMENT:
Create a list, [expression]-indexing.
Create an integer, [expression].
The [expression]) are described below:
Query: 1 x y
Find the sequence, [expression].
Append integer [expression].
Query: 2 x y
Find the sequence, [expression].
Find the value of element [expression].
Print the new value of [expression] on a new line
Task
Given [expression] queries, execute each query.
Note: [expression] is the bitwise XOR operation, which corresponds to the ^ operator in most languages. Learn more about it on Wikipedia.
----------
TOP SOLUTION:
----------
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, q;
int lastans = 0;
cin >> n >> q;
vector<vector<int>>sqces(n);
while (q--)
{
int a;
long long x, y;
cin >> a >> x >> y;
long long t = (x^lastans) % n;
if (a == 1)
{
sqces[t].push_back(y);
}
else
{
long long size = sqces[t].size();
long long b;
if (size != 0)
b = y%size;
else
continue;
cout << sqces[t][b] << endl;
lastans =sqces[t][b];
}
}
return 0;
}
----------
====================
----------
DATA-STRUCTURES.4
easy
----------
PROBLEM STATEMENT:
A left rotation operation on an array of size [expression].
Given an array of [expression] left rotations on the array. Then print the updated array as a single line of space-separated integers.
----------
TOP SOLUTION:
----------
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,d;
cin>>n>>d;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=d;i<n;i++){
cout<<a[i]<<" ";
}
for(int i=0;i<d;i++){
cout<<a[i]<<" ";
}
return 0;
}
----------
====================
----------
DATA-STRUCTURES.5
medium
----------
PROBLEM STATEMENT:
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings.
For example, given input [expression].
Function Description
Complete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in strings.
matchingStrings has the following parameters:
strings - an array of strings to search
queries - an array of query strings
----------
TOP SOLUTION:
----------
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<map>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
map<string, int>mp;
while (n--)
{
string t;
cin >> t;
mp[t]++;
}
int a;
cin >> a;
while (a--)
{
string t;
cin >> t;
cout << mp[t] << endl;
}
return 0;
}
----------
====================
----------
DATA-STRUCTURES.6
hard
----------
PROBLEM STATEMENT:
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
For example, the length of your array of zeros [expression]. Your list of queries is as follows:
[expression]
[expression]
[expression]
[expression]
Add the values of [expression] inclusive:
[expression]
[expression]
[expression]
[expression]
[expression]
The largest value is [expression] after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation has the following parameters:
n - the number of elements in your array
queries - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
----------
TOP SOLUTION:
----------
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long int64;
typedef pair<int, int> PII;
const int MOD = 1000000007;
const double EPSILON = 1e-10;
#define FORU(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define SIZE(A) ((int) A.size())
#define PB(X) push_back(X)
#define MP(A, B) make_pair(A, B)
template<class T> inline T tmin(T a, T b) {return (a < b) ? a : b;}
template<class T> inline T tmax(T a, T b) {return (a > b) ? a : b;}
template<class T> inline T tabs(T a) {return (a > 0) ? a : -a;}
template<class T> T gcd(T a, T b) {if (b == 0) return a; return gcd(b, a % b);}
struct Point {
int ind, type, val;
};
Point pt[400001];
bool comp(Point x, Point y)
{
if (x.ind != y.ind) return x.ind < y.ind;
return x.type < y.type;
}
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(false);
int N, M, k, x, y;
cin >> N >> M;
int cnt = 0;
REPU(i, 0, M) {
cin >> x >> y >> k;
pt[cnt].ind = x;
pt[cnt].type = 1;
pt[cnt++].val = k;
pt[cnt].ind = y+1;
pt[cnt].type = -1;
pt[cnt++].val = -k;
}
sort(pt, pt + cnt, comp);
int64 ans = 0, tot = 0;
REPU(i, 0, cnt) {
tot += pt[i].val;
ans = tmax(ans, tot);
}
cout << ans << endl;
return 0;
}
----------
====================
----------
DATA-STRUCTURES.7
easy
----------
PROBLEM STATEMENT:
This challenge is part of a MyCodeSchool tutorial track and is accompanied by a video lesson.
If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.
----------
TOP SOLUTION:
----------
/*
Print elements of a linked list on console
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
void Print(Node *head)
{
while(head!=NULL)
{
cout << head->data << "\n";
head = head->next;
}
}
----------
====================
----------
DATA-STRUCTURES.8
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.
----------
TOP SOLUTION:
----------
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Insert(Node *head,int data)
{
Node * temp=head;
Node * newN=new Node;
newN->next=NULL;
newN->data=data;
if(temp==NULL)
return newN;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newN;
return head;
}
----------
====================
----------
DATA-STRUCTURES.9
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.
----------
TOP SOLUTION:
----------
/*
Insert Node at the begining of a linked list
Initially head pointer argument could be NULL for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
return back the pointer to the head of the linked list in the below method.
*/
Node* Insert(Node *head,int data)
{
Node *newN=new Node;
newN->data=data;
newN->next=head;
return newN;
}
----------
====================
----------
DATA-STRUCTURES.10
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node.
A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.
As an example, if your list starts as [expression]
Function Description
Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.
insertNodeAtPosition has the following parameters:
head: a SinglyLinkedListNode pointer to the head of the list
data: an integer value to insert as data in your new node
position: an integer position to insert the new node, zero based indexing
----------
TOP SOLUTION:
----------
/*
Insert Node at a given position in a linked list
The linked list will not be empty and position will always be valid
First element in the linked list is at position 0
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* InsertNth(Node *head, int data, int position)
{
if(position == 0){
Node* a = (Node*)malloc(sizeof(Node));
a->data = data;
a->next = head;
return a;
}else{
int i;
Node* a = head;
for(i = 1; i < position; i++)
a = a->next;
Node* tmp = (Node*)malloc(sizeof(Node));
tmp->data = data;
tmp->next = a->next;
a->next = tmp;
return head;
}
}
----------
====================
----------
DATA-STRUCTURES.11
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
----------
TOP SOLUTION:
----------
/*
Delete Node at a given position in a linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Delete(Node *head, int position)
{
// Complete this method
Node *prev = NULL;
Node *ptr = head;
int pos = 0;
if(position==0)
{
head=head->next;
delete (ptr);
}
else
{
while(position!=pos)
{
++pos;
prev=ptr;
ptr=ptr->next;
}
if(ptr!=NULL)
{
prev->next=ptr->next;
delete (ptr);
}
}
return head;
}
----------
====================
----------
DATA-STRUCTURES.12
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You are given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, do not print anything!
----------
TOP SOLUTION:
----------
/*
Print elements of a linked list in reverse order as standard output
head pointer could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
void ReversePrint(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
if(head!=NULL)
{
ReversePrint(head->next);
cout<<head->data<<"\n";
}
}
----------
====================
----------
DATA-STRUCTURES.13
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list. Change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty.
----------
TOP SOLUTION:
----------
/*
Reverse a linked list and return pointer to the head
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Reverse(Node *head)
{
// Complete this method
Node* prev = NULL;
Node* current = head;
Node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
----------
====================
----------
DATA-STRUCTURES.14
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty.
----------
TOP SOLUTION:
----------
/*
Compare two linked lists A and B
Return 1 if they are identical and 0 if they are not.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int CompareLists(Node *headA, Node* headB)
{
// This is a "method-only" submission.
// You only need to complete this method
int flag=0;
while(headA != NULL && headB != NULL)
{
if(headA->data!=headB->data)
{
flag=1;
break;
}
headA = headA->next;
headB = headB->next;
}
if(flag==1 || headA!=NULL || headB!=NULL)
return 0;
else
return 1;
}
----------
====================
----------
DATA-STRUCTURES.15
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool
You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in ascending order. Either head pointer given may be null meaning that the corresponding list is empty.
----------
TOP SOLUTION:
----------
/*
Merge two sorted lists A and B as one linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* MergeLists(Node *a, Node*b)
{
// This is a "method-only" submission.
// You only need to complete this method
Node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->data <= b->data)
{
result = a;
result->next = MergeLists(a->next, b);
}
else
{
result = b;
result->next = MergeLists(a, b->next);
}
return(result);
}
----------
====================
----------
DATA-STRUCTURES.16
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool
You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.
----------
TOP SOLUTION:
----------
/*
Get Nth element from the end in a linked list of integers
Number of elements in the list will always be greater than N.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int GetNode(Node *head,int positionFromTail)
{
// This is a "method-only" submission.
// You only need to complete this method.
int count = 0;
Node *ptr = head;
while(ptr!=NULL)
{
++count;
ptr=ptr->next;
}
count = count-positionFromTail;
ptr=head;
--count;
while(count!=0)
{
--count;
ptr=ptr->next;
}
return ptr->data;
}
----------
====================
----------
DATA-STRUCTURES.17
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool
You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.
----------
TOP SOLUTION:
----------
/*
Remove all duplicate elements from a sorted linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* RemoveDuplicates(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
Node *ptr = head,*temp=NULL,*tmp=NULL;
while(ptr!=NULL && ptr->next!=NULL)
{
temp = ptr->next;
ptr->next=NULL;
while(temp!=NULL && ptr->data == temp->data)
{
tmp=temp;
temp=temp->next;
tmp->next=NULL;
delete(tmp);
}
ptr->next = temp;
ptr = temp;
}
return head;
}
----------
====================
----------
DATA-STRUCTURES.18
medium
----------
PROBLEM STATEMENT:
A linked list is said to contain a cycle if any node is visited more than once while traversing the list.
Complete the function provided for you in your editor. It has one parameter: a pointer to a Node object named [expression] that points to the head of a linked list. Your function must return a boolean denoting whether or not there is a cycle in the list. If there is a cycle, return true; otherwise, return false.
Note: If the list is empty, [expression] will be null.
----------
TOP SOLUTION:
----------
/*
Detect loop in a linked list
List could be empty also
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int HasCycle(Node* head)
{
// Complete this function
// Do not write the main method
Node *slowp = head, *fastp = head;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slowp == fastp)
{
return 1;
}
}
return 0;
}
----------
====================
----------
DATA-STRUCTURES.19
easy
----------
PROBLEM STATEMENT:
This challenge is part of a tutorial track by MyCodeSchool
Given pointers to the head nodes of [expression] linked lists that merge together at some point, find the Node where the two lists merge. It is guaranteed that the two head Nodes will be different, and neither will be NULL.
In the diagram below, the two lists converge at Node x:
[List #1] a---b---c
\
x---y---z---NULL
/
[List #2] p---q
Complete the int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) method so that it finds and returns the data value of the Node where the two lists merge.
----------
TOP SOLUTION:
----------
/*
Find merge point of two linked lists
Node is defined as
struct Node
{
int data;
Node* next;
}
*/
int getCount(Node* head)
{
Node* current = head;
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int getNode(int d, Node* head1, Node* head2)