Forward-Fill Nulls

MediumData EngineeringArray

Description

Given an array nums where some entries are null, replace each null with the most recent non-null value before it. A leading null (no prior value) stays null. Return the filled array.

Examples

Input:nums = [1,null,null,2,null]
Output:[1,1,1,2,2]
Explanation:

Carrying the last seen value forward over nulls yields [1,1,1,2,2].

Input:nums = [null,3,null]
Output:[null,3,3]
Explanation:

Carrying the last seen value forward over nulls yields [null,3,3].

Input:nums = [5,6,7]
Output:[5,6,7]
Explanation:

Carrying the last seen value forward over nulls yields [5,6,7].

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.