Showing Subcategory Name/Link Instead of Parent Category

Once you can’t know the order of categories returned by get_the_category, I suggest to loop through the categories and fill two arrays, one for parent and one for children.
After that if the parents array is empty showing the children, otherwise show the parents:

$categories = get_the_category();
if ( ! empty($categories) ) {
  $parents = array();
  $children = array();
  foreach( get_the_category() as $cat ) {
    if ( $cat->parent == 0 ) {
      $parents[] = $cat;
    } else {
      $children[] = $cat;
    }
  }
  $toshow = ! empty($children) ? $children : $parents;
  $sep = '';
  if ( ! empty($toshow) ) { foreach( $toshow as $cat ) {
    echo $sep;
    echo '<span class="' . $cat->name .'">';
    echo '<a href="' . get_category_link($cat->term_id) . '">' . $cat->name . '</a></span>';
    $sep = "https://wordpress.stackexchange.com/";
  } }
}