Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.
Locked until you have predicted 3 outputs correctly.
| 1 | struct Node { |
| 2 | int val; |
| 3 | Node* next; |
| 4 | }; |
| 5 | |
| 6 | Node* build(const vector<int>& values) { |
| 7 | |
| 8 | Node stub{0, nullptr}; |
| 9 | Node* tail = &stub; |
| 10 | |
| 11 | for (int v : values) { |
| 12 | tail->next = new Node{v, nullptr}; |
| 13 | tail = tail->next; |
| 14 | } |
| 15 | |
| 16 | return stub.next; |
| 17 | } |
| 18 | |
| 19 | vector<int> spill(Node* head) { |
| 20 | |
| 21 | vector<int> out; |
| 22 | |
| 23 | for (Node* p = head; p; p = p->next) { |
| 24 | out.push_back(p->val); |
| 25 | } |
| 26 | |
| 27 | return out; |
| 28 | } |
| 29 | |
| 30 | Node* drop(Node* head, int n) { |
| 31 | |
| 32 | Node stub{0, head}; |
| 33 | Node* lead = &stub; |
| 34 | Node* trail = &stub; |
| 35 | |
| 36 | for (int i = 0; i < n; i++) { |
| 37 | lead = lead->next; |
| 38 | } |
| 39 | |
| 40 | while (lead->next) { |
| 41 | lead = lead->next; |
| 42 | trail = trail->next; |
| 43 | } |
| 44 | |
| 45 | trail->next = trail->next->next; |
| 46 | |
| 47 | return stub.next; |
| 48 | } |
| 49 | |
| 50 | vector<int> solution(const vector<int>& values, int n) { |
| 51 | |
| 52 | Node* head = build(values); |
| 53 | |
| 54 | return spill(drop(head, n)); |
| 55 | } |
Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.