@liyun1988 It won't because the carry bit is added upfront.
" ListNode list = new ListNode(0);"
And it checks the value of carry bit in the end.
" return list.val == 0 ? list.next : list;"
It's a nice trick btw.
S
Shadowfax
@shadowfaxring
0
Reputation
4
Posts
81
Profile views
0
Followers
0
Following
Posts made by shadowfaxring

RE: Easy O(n) Java Solution using Stack

4 msec C++ solution w/o modifying the tree
vector<vector<int>> findLeaves(TreeNode* root) { vector<vector<int>> res; vector<int> leaves; unordered_set<TreeNode *> visited; visited.insert(NULL); while (visited.find(root) == visited.end()) { leaves.clear(); dfs(root, visited, leaves); res.push_back(leaves); } return res; } void dfs(TreeNode *root, unordered_set<TreeNode *> &visited, vector<int> &leaves) { if (visited.count(root)) return; if (visited.count(root>left) && visited.count(root>right)) { leaves.push_back(root>val); visited.insert(root); return; } dfs(root>left, visited, leaves); dfs(root>right, visited, leaves); }

RE: Java O(n) incredibly short yet easy to understand AC solution
Nice idea by using two pointers to construct the sorted array.

RE: C++ 6 lines easy understand solution.
Guess you do not check the existence of node p and q.