Two Sum
Brute Force: time O(n^2) space O(1)
Hash Map: time O(n) space O(n)
Add Two Numbers
Unconsidered:
[1]+[9,9]-> after l1 ends and l2 ends, there is still one carry
Struct initiation in C++: new ListNode(0)
if (temp->val==0) temp==0
change to
if (temp->val==0) {
l4 = l3;
while(l4->next!=NULL){
if(l4 ->next==temp) l4->next = NULL;
else l4=l4->next;
}
}
first one simply let temp points to NULL, but doesn't change the front node's next pointer, so l4 is needed as the front node then set its next to NULL.
网友评论