LeetCode Add Two Numbers
一道链表题,然后链表题的话其实它就是让你做一个模拟加法。然后它题意就是说给你两个非空的链表,注意是非空的,然后呢它的那个这个数字的每一位呢传输到它对应的结点上面,然后让你输出一个链表,然后就是代表了这个两个数字相加的和。而且它告诉你的话这个两个数的话是没有潜导零的,这样我们就比较方便地去处理一些边界,就不用处理一些边界问题。
然后我们这道题呢其实是有两个点要处理的,第一个点呢就是我们刚才说的是一个进位问题,还有一个呢就是它们两个链表的长度可能不一样,我们应该把所有的位数都给处理完,就是长度不一。
首先呢对于链表题的话呢我们,对于每道链表题我们推荐是都首先定义一个头结点,这个头呢主要是用于保存这个我们实际的头结点,就相当于这是一个哨兵结点,然后主要是用它的next结点去存储我们实际链表的头结点。这个赋值的话我们就直接赋1个1就行了,其实它是一个哨兵,主要是利用它的next结点。然后呢我们再用一个结点来指向一下我们这个当前要处理的这个新的这个链表,指向的是哪儿?比如我们这儿指向的肯定是,最开始的话我们这个新的链表的话肯定是指向的是头。然后因为它是非空的,所以它肯定不是最后就是说不可能是没有答案的,所以我们最后是要return,other than that,因为我们把实际的这个头结点是存储到它的next。然后根据进位的话那我们来定义一个carry位,这个初始化为零,然后我们就一位一位地处理。由于这边的l1跟l2的长度可能不一样,所以说我们处理的时候呢就是说必须得遍历到它们俩都不为空为止,所以的话我们这里推荐一个用一个大的while循环,就是这里
class Solution { public: //carry(进位),长度不一 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(-1);//哨兵 //ListNode* head = new ListNode(0);//哨兵 head->next = NULL; ListNode* current = head; int carry = 0; int ans = 0; //while (l1!= NULL && l2!= NULL) { while (l1 != NULL || l2 != NULL) { //while(l1||l2){ //head->val = l1->val + l2->val; //head->val = l1->val + l2->val + carry; //head->next = new ListNode(head->val); //current->val = l1->val + l2->val + carry; //if (l1 != NULL && l2 != NULL) { //ans = l1->val + l2->val + carry; //} /*if (l1 == NULL) { ans = l2->val + carry; //ans = l2->val; l2 = l2->next; } else if(l2 == NULL){ ans = l1->val + carry; //ans = l1->val; l1 = l1->next; }*/ ans = l1==NULL?l2->val+carry:l2==NULL?l1->val+carry:l2->val+l1->val+carry; //int l1val = (l1 != NULL) ? l1->val:0; //int l2val = (l2 != NULL) ? l2->val:0; //ans = l1val + l2val + carry; //current = new ListNode(l1->val + l2->val + carry); /*current = new ListNode(ans); current->next = head->next; head->next = current;*/ ListNode* p = new ListNode(ans % 10); current->next = p; //current->next = new ListNode(ans % 10); current = current->next; carry = 0; if (ans >= 10) { carry = 1; } //int sur=0; //if (l1!=NULL) { /*if (l1) { sur += l1->val; l1 = l1->next; }*/ //if (l2 != NULL) { /*if (l2) { sur += l2->val; l2 = l2->next; }*/ //head = head->next; //current=current->next; /*if (head->val >= 10){ head->val = head->val - 10; carry = 1;*/ //current = current->next; //head = head->next; //current = head->next; //head->val = head->val + 1; /*if (current->val >= 10){ current->val = current->val - 10; carry = 1; } else { carry = 0; }*/ //head->next = new ListNode(current->val); //current = head->next; //head = head->next; //current=head; if (l1 != NULL) { l1 = l1->next; } if (l2 != NULL) { l2 = l2->next; } } if (carry==1&&ans>=10) { ListNode* q = new ListNode(1); current->next = q; current = current->next; } return head->next; } };
#include "pch.h" #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: //carry(进位),长度不一 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(-1);//哨兵 //ListNode* head = new ListNode(0);//哨兵 head->next = NULL; ListNode* current = head; int carry = 0; int ans = 0; //while (l1!= NULL && l2!= NULL) { while (l1 != NULL || l2 != NULL) { //while(l1||l2){ //head->val = l1->val + l2->val; //head->val = l1->val + l2->val + carry; //head->next = new ListNode(head->val); //current->val = l1->val + l2->val + carry; //if (l1 != NULL && l2 != NULL) { //ans = l1->val + l2->val + carry; //} /*if (l1 == NULL) { ans = l2->val + carry; //ans = l2->val; l2 = l2->next; } else if(l2 == NULL){ ans = l1->val + carry; //ans = l1->val; l1 = l1->next; }*/ //ans = l1==NULL?l2->val+carry:l2==NULL?l1->val+carry:l2->val+l1->val+carry; //int l1val = (l1 != NULL) ? l1->val:0; //int l2val = (l2 != NULL) ? l2->val:0; int l1val = (l1 != NULL) ? l1->val : 0; int l2val = (l2 != NULL) ? l2->val : 0; ans = l1val + l2val + carry; //current = new ListNode(l1->val + l2->val + carry); /*current = new ListNode(ans); current->next = head->next; head->next = current;*/ ListNode* p = new ListNode(ans % 10); current->next = p; //current->next = new ListNode(ans % 10); current = current->next; carry = 0; if (ans >= 10) { carry = 1; } //int sur=0; //if (l1!=NULL) { /*if (l1) { sur += l1->val; l1 = l1->next; }*/ //if (l2 != NULL) { /*if (l2) { sur += l2->val; l2 = l2->next; }*/ //head = head->next; //current=current->next; /*if (head->val >= 10){ head->val = head->val - 10; carry = 1;*/ //current = current->next; //head = head->next; //current = head->next; //head->val = head->val + 1; /*if (current->val >= 10){ current->val = current->val - 10; carry = 1; } else { carry = 0; }*/ //head->next = new ListNode(current->val); //current = head->next; //head = head->next; //current=head; if (l1 != NULL) { l1 = l1->next; } if (l2 != NULL) { l2 = l2->next; } } if (carry==1&&ans>=10) { ListNode* q = new ListNode(1); current->next = q; current = current->next; } return head->next; } };