美文网首页
leetcode-day1

leetcode-day1

作者: 爆炸的热水袋 | 来源:发表于2018-08-28 22:58 被阅读0次

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.

相关文章

  • leetcode-day1

    Two Sum Brute Force: time O(n^2) space O(1) Hash Map: tim...

网友评论

      本文标题:leetcode-day1

      本文链接:https://www.haomeiwen.com/subject/rfzxwftx.html