Embercode
✕
Linked ListsQuick quiz

Intersection

6 questions. Covers Intersection of two lists.

Questions 1 to 5 of 6

  1. 1. In intersectionBySwitch, what does pa move to when it is nullptr?
  2. 2. Two separate nodes each hold the value 7, but they are different nodes in memory. Which statement is correct?
  3. 3. What does this program print? ``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; } ``
  4. 4. What extra memory does the length method use, apart from the lists themselves?
  5. 5. Two lists of 10,000 nodes each are checked with the naive method, which compares every node of A with every node of B. About how many steps does it take?

5 question(s) left on this page.