Easy Java solution using Stack and String Builder


  • 0
    P
    class Solution {
        public String reverseString(String s) {
            
            Stack<Character> stack = new Stack<>();
            
            for (char ch : s.toCharArray()){            
                stack.push(ch);            
            }
            
            StringBuilder sb = new StringBuilder();
            while (!stack.isEmpty()){            
                sb.append(stack.pop());            
            }
            
            return sb.toString();
            
            
        }
    }
    

Log in to reply
 

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