Simple Ruby solution


  • 0
    Y
    def merge (a, b)
      # merge b into a
      pointer = 0
      while pointer < a.length && b.any? do
        if b[0] < a[pointer]
          a.insert(pointer, b.shift)
        else
          pointer += 1
        end
      end
      
      a + b # whatever is left of b
    end
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.