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 | void rearrange(Node* head) { |
| 31 | |
| 32 | if (!head || !head->next) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | Node* slow = head; |
| 37 | Node* fast = head->next; |
| 38 | |
| 39 | while (fast && fast->next) { |
| 40 | slow = slow->next; |
| 41 | fast = fast->next->next; |
| 42 | } |
| 43 | |
| 44 | Node* back = slow->next; |
| 45 | slow->next = nullptr; |
| 46 | |
| 47 | Node* prev = nullptr; |
| 48 | |
| 49 | while (back) { |
| 50 | |
| 51 | Node* next = back->next; |
| 52 | |
| 53 | back->next = prev; |
| 54 | prev = back; |
| 55 | back = next; |
| 56 | } |
| 57 | |
| 58 | Node* front = head; |
| 59 | |
| 60 | while (prev) { |
| 61 | |
| 62 | Node* a = front->next; |
| 63 | Node* b = prev->next; |
| 64 | |
| 65 | front->next = prev; |
| 66 | prev->next = a; |
| 67 | |
| 68 | front = a; |
| 69 | prev = b; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | vector<int> solution(const vector<int>& values) { |
| 74 | |
| 75 | Node* head = build(values); |
| 76 | |
| 77 | rearrange(head); |
| 78 | |
| 79 | return spill(head); |
| 80 | } |
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.