Segmentation Fault

Your implementation was pretty messed up!
Keep it simple!
initialize temp inside for loop (i)
make sure to add null to v otherwise you may find some garbage values along.


after trying yourself, also see my approach.

Gotcha! Thanks man @mayankA47