it depends on the situation. if you talk a lot with them one on one, see them for multiple days, or mention lots of similar interests, then say it at a break in the conversation when it seems natural. you could also say "by the way, im john. (pause for strangers name and shake hands if desired) well then, see you around" right before you leave the train/elevator. if you don't see them a lot, then i'd say it isn't necessary to introduce yourself unless you have some sort of "strong bond".