input: Write a function to optimize the given Python code that sorts a list of numbers in O(n) time complexity. The function should take an unsorted list as input and return the sorted list.
  assert sort_list([2, 6, 3, 8, 4])==[2, 3, 4, 6, 8]
  assert sort_list([])==[]
  assert sort_list([9, 1, 5, 3, 7])==[1, 3, 5, 7, 9]
  assert sort_list([1, 1, 1, 1])==[1, 1, 1, 1]
  assert sort_list([-3, -1, -2, -4])==[-4, -3, -2, -1]
output: def sort_list(nums):
      if not nums:
          return []
      min_val = min(nums)
      max_val = max(nums)
      count = [0] * (max_val - min_val + 1)
      for num in nums:
          count[num - min_val] += 1
      sorted_list = []
      for i in range(len(count)):
          sorted_list.extend([i + min_val] * count[i])
      return sorted_list