6 questions. Covers Intersection of two lists.
Questions 1 to 5 of 6
intersectionBySwitch
pa
nullptr
cpp #include <iostream> struct Node { int value; Node* next = nullptr; }; Node* intersectionBySwitch(Node* a, Node* b, int& moves) { Node* pa = a; Node* pb = b; while (pa != pb) { pa = pa ? pa->next : b; pb = pb ? pb->next : a; ++moves; } return pa; } int main() { Node s2{8}; Node s1{7, &s2}; Node a4{4, &s1}; Node a3{3, &a4}; Node a2{2, &a3}; Node a1{1, &a2}; Node b1{9, &s1}; int moves = 0; Node* hit = intersectionBySwitch(&a1, &b1, moves); std::cout << "value " << hit->value << " after " << moves << " moves\n"; return 0; }
5 question(s) left on this page.