LeetCode 151 Reverse Words in a String

    /**
     * @param {string} s
     * @return {string}
     */
    var reverseWords = function(s) {
        var reversed = '';
        var segment = '';
        for (var i = 0; i < s.length; i++) {
            if (s[i] === ' ') {
                if ((s[i - 1] == null || s[i + 1] == null)) {
                    segment = null;
                } else if (segment != null) {
                    segment = ' ';
                }
                
                if (s[i + 1] !== ' ') {
                    if (segment) {
                        reversed = segment + reversed;
                    }
                    segment = '';
                }
            } else {
                segment += s[i];
                if (s[i + 1] === ' ' || s[i + 1] == null) {
                    reversed = segment + reversed;
                    segment = '';
                }
            }
        }
        
        return reversed;
    };

@L42y
:+1: