[LeetCode] Add Two Numbers

简介: The idea is to add the two numbers (represented by linked lists) nodes after nodes and store the result in the longer list.

The idea is to add the two numbers (represented by linked lists) nodes after nodes and store the result in the longer list. Since we may append the additioanl carry bit after the longer list, we need to be careful not to reach the NULL pointer of the longer list.

The code is as follows. In fact, it is of time O(length(l1) + length(l2)) (the two calls oflistLength) and includes two-pass, but it is very fast in the OJ :-)

The code is as follows, about 20 lines. You may run it on the following examples and then you will understand the details of the code.

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 4         if (listLength(l1) < listLength(l2))
 5             return addTwoNumbers(l2, l1);
 6         ListNode *r1 = l1, *r2 = l2;
 7         int c = 0;
 8         bool isEnd = false;
 9         while (r2) {
10             int val = r1 -> val + r2 -> val + c;
11             r1 -> val = val % 10;
12             c = val / 10;
13             if (r1 -> next) r1 = r1 -> next;
14             else isEnd = true;
15             r2 = r2 -> next;
16         }
17         while (c) {
18             int val = isEnd ? c : r1 -> val + c;
19             if (isEnd) r1 -> next = new ListNode(val % 10);
20             else r1 -> val = val % 10;
21             c = val / 10;
22             if (r1 -> next) r1 = r1 -> next;
23             else isEnd = true;
24         }
25         return l1;
26     }
27 private:
28     int listLength(ListNode* head) {
29         return head ? 1 + listLength(head -> next) : 0;
30     }
31 };

Examples:

  1. l1 = 5 -> NULL, l2 = 5 -> NULL;
  2. l1 = 1 -> NULL, l2 = 9 -> 9 -> NULL;
  3. l1 = 8 -> 9 -> NULL, l2 = 1 - >NULL.

Well, you guess what? Yeah, these are just the test cases which my code gives mistake before the debugging :-)

目录
相关文章
|
存储 缓存 算法
LeetCode刷题---Two Sum(一)
LeetCode刷题---Two Sum(一)
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
115 0
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
123 0
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
215 0
LeetCode - #2 Add Two Numbers
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
152 0
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
|
存储 算法
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
下一篇
oss云网关配置