Based on Stone-Wales (SW) defect evolution theory and molecular dynamics, we simulate the docking process of two caped carbon nanotubes (CNTs) of different types to form a heterojunction using Monte Carlo methods. First, an algorithm for a fast simulation of the cap change in CNTs is put forward and the cap formation of single CNTs with open ends is simulated, by applying this method. SW defect evolution is designed as a leap change simulation of these caps, represents C-C bond formation and breakage, while molecular dynamics is used to simulate the gradient change of the relative bond distance between the C atoms. The coalescence process of forming heterojunction is also studied here. These simulations show that the process of docking is first to generate a large number of defects, which will precipitate the coalescence, then many defects disappear through the compound, finally the remaining defects transfer to the ends of this heterojunction in the form of pentagon/heptagon rings, thus leading to the reduced overall energy.