```
public class Solution {
//analyze the pattern
//n=0 -> 0
//n=1 -> 0, 1
//n=2 -> (00, 01), (10, 11)
//n=3 -> (000, 001, 010, 011), (111, 110, 101, 100)
//so the pattern is when n=n -> add 0 in front of all the result of (n-1)'s binary value (This is just same as all the result of (n-1)
// and add 1 in front of all the result of(n-1)'s binary value (This need to calculate.)
public List<Integer> grayCode(int n) {
List<Integer> result = new ArrayList();
result.add(0);
for(int i=1; i<=n; i++){
int front=1;
//Create the correct value for binary format (10...0) which the value has i digi
//so shift 1 to right (i-1) times
for(int j=1; j<i; j++){
front = front<<1;
}
//add the new generated value to the result list
//the new generated value is the last result add front value
int size=result.size();
//we want to loop through the (n-1) result from end to start. This is just because want to make the test case match the Leetcode answer. You can use other way loop through the (n-1) result.
for(int k=size-1; k>=0; k--){
result.add(result.get(k)+front);
}
}
return result;
}
```

}