Heavy ion collisions provide a unique opportunity to study the nature of X(3872) compared with electron-positron and proton-proton (antiproton) collisions. With the abundant charm pairs produced in heavy-ion collisions, the production of multicharm hadrons and molecules can be enhanced by the combination of charm and anticharm quarks in the medium. We investigate the centrality and momentum dependence of X(3872) in heavy-ion collisions via the Langevin equation and instant coalescence model (LICM). When X(3872) is treated as a compact tetraquark state, the tetraquarks are produced via the coalescence of heavy and light quarks near the quantum chromodynamic (QCD) phase transition due to the restoration of the heavy quark potential at $Trightarrow T_c$. In the molecular scenario, loosely bound X(3872) is produced via the coalescence of $D^0$-$bar D^{*0}$ mesons in a hadronic medium after kinetic freeze-out. The phase space distributions of the charm quarks and D mesons in a bulk medium are studied with the Langevin equation, while the coalescence probability between constituent particles is controlled by the Wigner function, which encodes the internal structure of the formed particle. First, we employ the LICM to explain both $D^0$ and $J/psi$ production as a benchmark. Then, we give predictions regarding X(3872) production. We find that the total yield of tetraquark is several times larger than the molecular production in Pb-Pb collisions. Although the geometric size of the molecule is huge, the coalescence probability is small due to strict constraints on the relative momentum between $D^0$ and $bar D^{*0}$ in the molecular Wigner function, which significantly suppresses the molecular yield.