All we need to do is to add an '1' to the top digit of the binary string and reversely added the new number to the list.

```
public List<Integer> grayCode(int n) {
List<Integer> ans = new ArrayList<>();
ans.add(0);
for(int i = 0; i < n ; i++)
for(int j = ans.size()-1; j>=0; j--)
ans.add(ans.get(j)+(1<<i));
return ans;
}
```