Reference solution
One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node* next;
Node(int v) : val(v), next(nullptr) {}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
Node* head = nullptr;
Node* tail = nullptr;
for (int i = 0; i < n; i++) {
int x; cin >> x;
Node* node = new Node(x);
if (!head) { head = tail = node; }
else { tail->next = node; tail = node; }
}
int x; cin >> x; // insert at head
Node* h = new Node(x);
h->next = head; head = h;
for (Node* cur = head; cur; cur = cur->next) {
if (cur != head) cout << ' ';
cout << cur->val;
}
cout << '\n';
return 0;
}