leetcode刷题: 002 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8

首先使用链表实现等长无进位的求和,即实现 (1 —> 2 -> 3) + (1 -> 2 -> 3)=(2 -> 3 -> 6)

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 struct ListNode {
 6      int val;
 7      ListNode *next;
 8      ListNode(int x) : val(x), next(NULL) {}
 9 };
10
11
12 class Solution {
13 public:
14     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
15
16         int val1 = 0, val2 = 0;
17
18         ListNode *l = NULL;
19         ListNode *l_end = NULL;
20         ListNode *l_new = NULL;
21
22         val1 = l1->val;
23         val2 = l2->val;
24         l1 = l1->next;
25         l2 = l2->next;
26         l_new = (ListNode *)new ListNode((val1 + val2) % 10);
27         l_new->next = NULL;
28         l = l_new;
29         l_end = l;
30         while (l1 != NULL || l2 != NULL){
31
32                 val1 = l1->val;
33                 val2 = l2->val;
34                 l1 = l1->next;
35                 l2 = l2->next;
36
37             l_new = (ListNode *)new ListNode((val1 + val2)%10);
38             l_new->next = NULL;
39
40             l_end->next = l_new;
41             l_end = l_new;
42         }
43
44         return l;
45
46     }
47 };
48
49 int main()
50 {
51     ListNode *l1, *l2;
52     ListNode *l;
53     l1 = (ListNode *)new ListNode(1);
54     l2 = (ListNode *)new ListNode(1);
55     l1->next = (ListNode *)new ListNode(2);
56     l2->next = (ListNode *)new ListNode(2);
57     l1->next->next = (ListNode *)new ListNode(3);
58     l2->next->next = (ListNode *)new ListNode(3);
59     Solution s;
60     l = s.addTwoNumbers(l1, l2);
61     while (l != NULL){
62         cout << l->val << endl;
63         l = l->next;
64     }
65     while (1);
66 }

运行结果:

然后实现不等长无进位的求和,即实现 (1 —> 2 -> 3) + (1)=(2 -> 2 -> 3)

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 struct ListNode {
 6      int val;
 7      ListNode *next;
 8      ListNode(int x) : val(x), next(NULL) {}
 9 };
10
11
12 class Solution {
13 public:
14     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
15
16         int val1 = 0, val2 = 0;
17
18         ListNode *l = NULL;
19         ListNode *l_end = NULL;
20         ListNode *l_new = NULL;
21
22         val1 = l1->val;
23         val2 = l2->val;
24         l1 = l1->next;
25         l2 = l2->next;
26         l_new = (ListNode *)new ListNode((val1 + val2) % 10);
27         l_new->next = NULL;
28         l = l_new;
29         l_end = l;
30         while (l1 != NULL || l2 != NULL){
31             if (l1 == NULL){
32                 val2 = l2->val;
33                 l2 = l2->next;
34                 val1 = 0;
35             }
36             else if (l2 == NULL){
37                 val1 = l1->val;
38                 l1 = l1->next;
39                 val2 = 0;
40             }
41             else{
42                 val1 = l1->val;
43                 val2 = l2->val;
44                 l1 = l1->next;
45                 l2 = l2->next;
46             }
47
48             l_new = (ListNode *)new ListNode((val1 + val2)%10);
49             l_new->next = NULL;
50
51             l_end->next = l_new;
52             l_end = l_new;
53         }
54
55         return l;
56
57     }
58 };
59
60 int main()
61 {
62     ListNode *l1, *l2;
63     ListNode *l;
64     l1 = (ListNode *)new ListNode(1);
65     l2 = (ListNode *)new ListNode(1);
66     l1->next = (ListNode *)new ListNode(2);
67     //l2->next = (ListNode *)new ListNode(2);
68     l1->next->next = (ListNode *)new ListNode(3);
69     //l2->next->next = (ListNode *)new ListNode(3);
70     Solution s;
71     l = s.addTwoNumbers(l1, l2);
72     while (l != NULL){
73         cout << l->val << endl;
74         l = l->next;
75     }
76     while (1);
77 }

运行结果:

最后实现不等长有进位的求和,即实现题目要求(注意最后一位有进位的情况)

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 struct ListNode {
 6      int val;
 7      ListNode *next;
 8      ListNode(int x) : val(x), next(NULL) {}
 9 };
10
11 class Solution {
12 public:
13     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14
15         int val1 = 0, val2 = 0, carry = 0;
16
17         ListNode *l = NULL;
18         ListNode *l_end = NULL;
19         ListNode *l_new = NULL;
20
21         val1 = l1->val;
22         val2 = l2->val;
23         l1 = l1->next;
24         l2 = l2->next;
25         l_new = (ListNode *)new ListNode((val1 + val2 + carry) % 10);
26         l_new->next = NULL;
27         l = l_new;
28         carry = (val1 + val2 + carry) / 10;
29         l_end = l;
30         while (l1 != NULL || l2 != NULL){
31             if (l1 == NULL){
32                 val2 = l2->val;
33                 l2 = l2->next;
34                 val1 = 0;
35             }
36             else if (l2 == NULL){
37                 val1 = l1->val;
38                 l1 = l1->next;
39                 val2 = 0;
40             }
41             else{
42                 val1 = l1->val;
43                 val2 = l2->val;
44                 l1 = l1->next;
45                 l2 = l2->next;
46             }
47
48             l_new = (ListNode *)new ListNode((val1 + val2 + carry)%10);
49             l_new->next = NULL;
50             carry = (val1 + val2 + carry) / 10;
51
52             l_end->next = l_new;
53             l_end = l_new;
54         }
55         if (carry != 0){
56             l_new = (ListNode *)new ListNode(carry);
57             l_new->next = NULL;
58             l_end->next = l_new;
59             l_end = l_new;
60         }
61         return l;
62
63     }
64 };
65
66 int main()
67 {
68     ListNode *l1, *l2;
69     ListNode *l;
70     l1 = (ListNode *)new ListNode(1);
71     l2 = (ListNode *)new ListNode(1);
72     l1->next = (ListNode *)new ListNode(2);
73     l2->next = (ListNode *)new ListNode(9);
74     l1->next->next = (ListNode *)new ListNode(2);
75     l2->next->next = (ListNode *)new ListNode(7);
76     Solution s;
77     l = s.addTwoNumbers(l1, l2);
78     while (l != NULL){
79         cout << l->val << endl;
80         l = l->next;
81     }
82     while (1);
83 }

运行结果:

因为是一边学C++,一边刷leetcode,所以有什么问题,十分感谢您能指点。

时间: 2024-11-11 12:48:31

leetcode刷题: 002 Add Two Numbers的相关文章

LeetCode第二题:Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

刷题2. Add Two Numbers

一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two

LeetCode刷题笔录Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 具体一位一位地加就行了,考虑进位的问题.还有最后记得把生成的string反过来再返回,因为我们是从最低位开始加的. public class Solution { public String addBinary(String a

【leetcode刷题笔记】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

【leetcode刷题笔记】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: 1 public class Solution { 2 public Str

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上