题目描述
实现代码
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {int addNumber = 0;ListNode* res = ListNode();ListNode* curNode = res;while(l1 && l2){curNode.value = (l1.value + l2.value + addNumber) % 10 addNumber = (l1.value + l2.value) / 10;curNode.next = *ListNode();curNode = curNode.next;l1 = l1.next;l2 = l2.next;}while(l1){curNode.value = (l1.value + addNumber) % 10 addNumber = l1.value / 10;curNode.next = *ListNode();curNode = curNode.next;l1 = l1.next;}while(l2){curNode.value = (l2.value + addNumber) % 10 addNumber = l2.value / 10;curNode.next = *ListNode();curNode = curNode.next;l2 = l2.next;}return res;}
分析总结
- unordered_map是使用哈希表,时间复杂度是O(1)