002. Add Two Numbers

Posted ming-1012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了002. Add Two Numbers相关的知识,希望对你有一定的参考价值。

题目链接:https://leetcode.com/problems/add-two-numbers/description/

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

 

思路:

  • 用两个结点指针p1、p2分别指向这两个链表L1、L2
  • p1、p2两者指向非空时将p1、p2所指向结点的值val和进位数carNum相加。即sum = p1->val + p2->val + carNum; 其中进位数carNum初始值为0。
    1. 更新进位数的值:carNum = sum / 10;
    2. 获得当前位的值:cur = sum % 10;
  • p1 p2 两者指向至少有一个为空时,上一步的操作终止。对非空的链表继续进行后续操作。

     

编码如下

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12         ListNode *p1 = l1;
13         ListNode *p2 = l2;
14         
15         ListNode *pHead = new ListNode(-1);
16         ListNode *p = nullptr;
17         
18         
19         int carNum = 0;     // 进位数
20         
21         while (nullptr != p1 && nullptr != p2)
22         {
23             int sum = p1->val + p2->val + carNum;
24             carNum = sum / 10;      // 求进位数
25             int cur = sum % 10;     // 当前位的数字
26             
27             ListNode *pTemp = new ListNode(cur);
28             
29             if (p == nullptr)
30             {
31                 p = pTemp;
32                 pHead->next = p;
33             }
34             else
35             {
36                 p->next = pTemp;
37                 p = pTemp;
38             }
39             
40             p1 = p1->next;
41             p2 = p2->next;
42         }
43         
44         
45         while (nullptr != p1)
46         {
47             int sum = p1->val + carNum;
48             carNum = sum / 10;
49             int cur = sum % 10;
50             
51             ListNode *pTemp = new ListNode(cur);
52             
53             if (p != nullptr)
54             {
55                 p->next = pTemp;
56                 p = pTemp;
57             }
58             
59             p1 = p1->next;
60         }
61         
62         
63          while (nullptr != p2)
64         {
65             int sum = p2->val + carNum;
66             carNum = sum / 10;
67             int cur = sum % 10;
68             
69             ListNode *pTemp = new ListNode(cur);
70             
71             if (p != nullptr)
72             {
73                 p->next = pTemp;
74                 p = pTemp;
75             }
76             
77             p2 = p2->next;
78         }
79         
80         if (carNum != 0)
81         {
82             ListNode *pTemp = new ListNode(carNum);
83             if (p != nullptr)
84             {
85                 p->next = pTemp;
86                 p = pTemp;
87             }
88         }
89         
90         return pHead->next;
91         
92     }
93 };

 

 

  

以上是关于002. Add Two Numbers的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode002 Add Two Numbers

LeetCode 002 Add Two Numbers - Java

LeetCode刷题系列 - 002题Add Two Numbers

乘风破浪:LeetCode真题_002_Add Two Numbers

链表-Add Two Numbers

Add Two Numbers