I think locust is an appropriate way as well since you cant select the middle segments that way (they are not targetable anyways). But yea invulnerable is ok too I guess
Well, then, you could do that, when a new segment is created, store him in a unit array (let say; LastSegment ) with the index responding to the correct player number of the killing unit. But before that, remove the locust/invu ability from the current last segment. Then, simply "Set LastSegment[player number of owner of killing unit] = Last created unit". So by that, you could keep track of the right final segment.
Edit: this is in the same path as numerion suggests. However it has to be a unit array (or two), since it otherwise would override any worm's last segment with their last one.