```
public class Solution {
public int longestValidParentheses(String s) {
if(s == null || s.length() <= 1){
return 0;
}
int start = -1;
int res = 0;
LinkedList<Integer> stack = new LinkedList<Integer>();
for(int i=0;i<s.length();i++){
if(s.charAt(i) == '('){
stack.push(i);
}else{
if(!stack.isEmpty()){
stack.pop();
if(!stack.isEmpty()){
res = Math.max(res,i-stack.peek());
}else{
res = Math.max(res,i-start);
}
}else{
start = i;
}
}
}
return res;
}
```

}