I was wondering why the code would throw an error: incompatible types: List cannot be converted to LinkedList.
However, it could run when I change the method helper to:
public void helper(String curr, List res,int left,int right,int n){
or I change the code that calls the constructor: LinkedList res = new LinkedList();
class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new LinkedList<String>();
helper("",res,0,0,n);
return res;
}
public void helper(String curr, LinkedList<String> res,int left,int
right,int n){
if(right==n) {res.add(curr); return;}
if(left<n) helper(curr+"(",res,left+1,right,n);
if(right<left) helper(curr+")",res,left,right+1,n);
}
}
List<String>- ernest_kList<String>might not necessary be aLinkedList<String>, right? It could be anArrayList<String>or some other kind of list. What don't you understand? - SweeperLinkedList, which means you cannot pass any other implementation ofListor the super type itself. - deHaar