in temp you are taking the data to be inserted in the linked list .

so you are attaching that node as the next node of q here