Insert Into Sub-child Menu

Check that you actually have categories before creating the list, and move the lines that echo the <ul> inside the conditional.

$categories = get_the_category();
if (!empty($categories)) {
  foreach($categories as $category){
    $parent = $category->parent;
    if($parent != 0){
      echo '<ul style="background:#000">';
        wp_list_categories("child_of={$parent}&title_li");
      echo '</ul>';
    }
  }
}

This will generate multiple lists, not just one.

Here is a version that echoes only a single <ul>.

$categories = get_the_category();
$catli = '';
if (!empty($categories)) {
  foreach($categories as $category){
    $parent = $category->parent;
    if($category->parent != 0){
      $catli .= wp_list_categories("child_of=$parent&title_li&echo=0");
    }
  }
  if (!empty($catli)) {
    echo '<ul style="background:#000">'.$catli.'</ul>';
  }
}