2. Add Two Numbers

Posted yuanninesuns

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 numbers do not contain any leading zero, except the number 0 itself.

第一次尝试代码:

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

错误点:

1.我在这里直接用ans = ans->next,但是事实上一开始初始化的时候ans->next是指向NULL的,所以这里会报错。需要用ans->next = new ListNode(answer);

2.函数体最后直接return ans是不对的,因为这时候ans代表的结构体是,val=8;next->NULL。所以最后答案只会打印出一个数字。需要用到另一个结构体flag,一开始对其进行赋值,令ans=flag,让flag记录一下最开始的地址,最后令ans=flag->next即可。

 

第二次尝试代码:

 1 #include<iostream>
 2 #define NULL 0 
 3 using namespace std;
 4 struct ListNode {
 5      int val;
 6      ListNode *next;
 7      ListNode(int x) : val(x), next(NULL) {}
 8  };
 9 
10 class Solution {
11 public:
12     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
13         ListNode* flag = new ListNode(0);
14 
15         ListNode* ans = flag;
16 
17         ListNode *p = l1;
18         ListNode *q = l2;
19         ListNode *temp = new ListNode(0);
20 
21         int a = 0;
22         int answer = 0;
23 
24         while (p != NULL&&q != NULL) {
25 
26             if (p->val + q->val + a >= 10) {
27                 answer = p->val + q->val - 10 + a;
28             
29                 ans->next = new ListNode(answer);
30                 
31                 a = 1;
32             }
33             else {
34                 answer = p->val + q->val + a;
35 
36                 ans->next = new ListNode(answer);
37 
38                 a = 0;
39             }
40             p = p->next;
41             q = q->next;
42         }
43         if (p == NULL&&q != NULL) {
44             ListNode *temp = q;
45         }
46         if(q == NULL&&p != NULL){
47             ListNode *temp = p;
48         }
49         else{ 
50             if (a == 1) {
51                 ans->next = new ListNode(1);
52             }
53             cout << "return" << endl;
54             cout << "flag->next " << flag->next->val << endl;
55             ans = flag->next;
56             cout << "ans " << ans->val << endl;
57             delete flag;
58             return ans;
59         }
60 
61         
62 
63         while(temp->next != NULL) {
64             answer = temp->val + a;
65             ans->next = new ListNode(answer);
66         }
67         cout << "here" << endl;
68         ans = flag->next;
69         delete flag;
70         return ans;
71     }
72 };
73 
74 int main() {
75     Solution my;
76     ListNode *l1=new ListNode(2);
77     l1->next = new ListNode(4);
78     l1->next->next = new ListNode(3);
79     cout << "l1 " << l1->val << endl;
80 
81 
82     ListNode *l2 = new ListNode(5);
83     l2->next = new ListNode(6);
84     l2->next->next = new ListNode(4);
85     cout << "l2 " << l2->val << endl;
86 
87     cout << "main" << endl;
88     ListNode* ans = my.addTwoNumbers(l1, l2);
89     while (ans != NULL) {
90         cout << "答案" << endl;
91         cout << ans->val << endl;
92         ans = ans->next;
93     }
94     return 0;
95 }

错误点:

这里有一个指针含义的误区。在一开始ListNode* ans = flag这个赋值语句时,ans和flag其实就是一个东西,val=0;next->NULL。就相当于一个房间的两个钥匙,当ans->next = new ListNode(answer)后,这个房间中的东西发生变化,val=0;next->一个确切的地址。这时候ans和flag还是同一个东西。当ans=ans->next后,ans发生变化,但是flag还是没有变化,即flag->next指向的是题目中7这个数所在的结点,是链表的开始。所以最后ans=flag->next可以找回整条链表。

而这里的错误点是在每次计算时没有令ans=ans->next,使得每次改变的数都是ans->next,所以最后只能打印出一个一个值。

 

第三次尝试代码:

#include<iostream>
#define NULL 0 
using namespace std;
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };

class Solution {
public:
	ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
		ListNode* flag = new ListNode(0);
		ListNode* ans = flag;
		ListNode *p = l1;
		ListNode *q = l2;
		ListNode *temp = new ListNode(0);
		int a = 0;
		int answer = 0;
		while (p != NULL&&q != NULL) {
			if (p->val + q->val + a >= 10) {
				answer = p->val + q->val - 10 + a;
				ans->next = new ListNode(answer);
				a = 1;
			}
			else {
				answer = p->val + q->val + a;
				ans->next = new ListNode(answer);
				a = 0;
			}
			p = p->next;
			q = q->next;
			ans = ans->next;
		}
		if (p == NULL&&q != NULL) {
			ListNode *temp = q;
		}
		if(q == NULL&&p != NULL){
			ListNode *temp = p;
		}
		else{ 
			if (a == 1) {
				ans->next = new ListNode(1);
			}
			cout << "return" << endl;
			cout << "flag " << flag->val << endl;
			cout << "flag->next " << flag->next->val << endl;
			//cout << "flag->next->next " << flag->next->next->val << endl;
			ans = flag->next;
			cout << "ans " << ans->val << endl;
			//cout << "ans->next " << ans->next->val << endl;
			delete flag;
			cout << "ans2 " << ans->val << endl;
			return ans;
		}
		while(temp->next != NULL) {
			answer = temp->val + a;
			ans->next = new ListNode(answer);
		}
		cout << "here" << endl;
		ans = flag->next;
		delete flag;
		return ans;
	}
};

int main() {
	Solution my;
	ListNode *l1=new ListNode(2);
	l1->next = new ListNode(4);
	l1->next->next = new ListNode(3);
	cout << "l1 " << l1->val << endl;

	ListNode *l2 = new ListNode(5);
	l2->next = new ListNode(6);
	l2->next->next = new ListNode(4);
	cout << "l2 " << l2->next->next->val << endl;

	cout << "main" << endl;
	ListNode* ans = my.addTwoNumbers(l1, l2);
	while (ans != NULL) {
		cout << "答案" << endl;
		cout << ans->val << endl;
		ans = ans->next;
	}
	return 0;
}

错误点:

1.当输入【1,8】和【0】时返回的应该是【1,8】,但这个函数返回的却是【1】,不符合规定。

2.当输入【9,9】和【1】时返回的应该是【1,0,0】,不符合规定。

第四次尝试代码:

  1 #include<iostream>
  2 #define NULL 0 
  3 using namespace std;
  4 struct ListNode {
  5      int val;
  6      ListNode *next;
  7      ListNode(int x) : val(x), next(NULL) {}
  8  };
  9 
 10 class Solution {
 11 public:
 12     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 13         ListNode* ans = new ListNode(0);
 14         ListNode* flag = ans;
 15         ListNode *p = l1;
 16         ListNode *q = l2;
 17         ListNode *temp = new ListNode(0);
 18         int a = 0;
 19         int answer = 0;
 20         while (p != NULL&&q != NULL) {
 21             if (p->val + q->val + a >= 10) {
 22                 answer = p->val + q->val - 10 + a;
 23                 ans->next = new ListNode(answer);
 24                 a = 1;
 25             }
 26             else {
 27                 answer = p->val + q->val + a;
 28                 ans->next = new ListNode(answer);
 29                 a = 0;
 30             }
 31             p = p->next;
 32             q = q->next;
 33             ans = ans->next;
 34         }
 35         if (p == NULL&&q != NULL) {
 36             temp = q;
 37             cout << "aaaa" << endl;
 38             cout << "1,8:" << endl;
 39             cout << "q: " << q->val << endl;
 40         }
 41         else if (q == NULL&&p != NULL){
 42             temp = p;
 43             cout << "0:" << endl;
 44             cout << "p: " << temp->val<< endl;
 45         }
 46         else {
 47             if (a == 1) {
 48                 ans->next = new ListNode(1);
 49             }
 50             cout << "return" << endl;
 51             ans = flag->next;
 52             delete flag;
 53             return ans;
 54         }
 55         while(temp != NULL) {
 56             cout << "temp->val: " << temp->val << endl;
 57             if (temp->val + a >= 10) {
 58                 answer = temp->val -10 + a;
 59                 ans->next = new ListNode(answer);
 60                 a = 1;
 61             }
 62             else {
 63                 answer = temp->val + a;
 64                 ans->next = new ListNode(answer);
 65                 a = 0;
 66             }
 67             ans = ans->next;
 68             temp = temp->next;
 69         }
 70         if (a == 1) {
 71             ans->next = new ListNode(1);
 72         }
 73         cout << "here" << endl;
 74 
 75         ans = flag->next;
 76 
 77         delete flag;
 78 
 79         return ans;
 80     }
 81 };
 82 
 83 int main() {
 84     Solution my;
 85     ListNode *l1=new ListNode(9);
 86     l1->next = new ListNode(9);
 87 
 88     cout << "l1 " << l1->next->val << endl;
 89 
 90     ListNode *l2 = new ListNode(1);
 91     cout << "l2 " << l2->val << endl;
 92 
 93     cout << "main" << endl;
 94     ListNode* ans = my.addTwoNumbers(l1, l2);
 95     while (ans != NULL) {
 96         cout << "答案" << endl;
 97         cout << ans->val << endl;
 98         ans = ans->next;
 99     }
100     return 0;
101 }

笔记:

1.对于链表,新增加一个结点时的操作l->next=new ListNode(1)

2.计算结束后指针会指向链表结尾,这时候需要在初始化时就创建一个结点保存链表初始位置

3.就对这道题而言,不同位数相加,最后进位需要注意,如99+1

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

Add Two Numbers

2. Add Two Numbers

2_Add Two Numbers --LeetCode

Leetcode 2. Add Two Numbers

Leetcode 2. Add Two Numbers

我的LC之路-2. Add Two Numbers