I wrote a function that gets rid of consecutive spaces with a single space without the use of replace functions. However, I am confused about the time complexity of the function/if there is a better way to implement it.

```
static String removeSpaces(String s){
    StringBuffer sb = new StringBuffer();
    if (s == null){
        return "";
    }
    if (s.length() == 0){
        return "";
    }
    for (int i=0;i<s.length();i++){
        if (s.charAt(i) == ' '){
            sb.append(" ");
            if (i < s.length() - 1){
                while (s.charAt(i) == ' '){
                    i++;
                }
            }
        }
        sb.append(s.charAt(i));

    }
    return sb.toString();
}

```