Gives two non-empty chains to represent two non-negative integers.Where their respective digits are stored in reverse order, and each of their nodes can only store one digit.
If we add the two numbers together, a new list of chains is returned to represent their sum.
You can assume that neither number starts with 0 except the number 0.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Reason: 342 + 465 = 807
The program code is as follows:
#include <iostream> #include <cstdio> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* addTwoNumbers(ListNode* l1, ListNode* l2); int main() { int m,n; cin>>m>>n; int temp_m; cin>>temp_m; ListNode *head_m = new ListNode(temp_m); ListNode *index = head_m; for(int i = 0;i<m-1;++i) { cin>>temp_m; ListNode *new_m_node = new ListNode(temp_m); index->next = new_m_node; index = new_m_node; } int temp_n; cin>>temp_n; ListNode*head_n = new ListNode(temp_n); index = head_n; for(int i = 0;i<n-1;++i) { cin>>temp_n; ListNode *new_n_node = new ListNode(temp_n); index->next = new_n_node; index = new_n_node; } ListNode *ans = addTwoNumbers(head_m, head_n); while (ans->next!=NULL) { cout<<ans->val<<endl; ans = ans->next; } cout<<ans->val; return 0; } ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { bool flag = true; ListNode *ans = NULL; ListNode *ans_head = NULL; bool ifadd = false; while (l1!=NULL&&l2!=NULL) { if(!ifadd) { if(flag) { flag = false; ans = new ListNode((l1->val+l2->val)%10); ans_head = ans; } else { ans->next = new ListNode((l1->val+l2->val)%10); ans = ans->next; } if((l1->val+l2->val)/10==1) ifadd = true; l1 = l1->next; l2 = l2->next; } else { if(flag) { flag = false; ans = new ListNode((l1->val+l2->val+1)%10); ans_head = ans; } else { ans->next = new ListNode((l1->val+l2->val+1)%10); ans = ans->next; } if((l1->val+l2->val+1)/10==0) ifadd = false; l1 = l1->next; l2 = l2->next; } } if(l1==NULL&&l2==NULL) { if (ifadd) { ans->next = new ListNode(1); } else { return ans_head; } } if(l1==NULL&&l2!=NULL) { l1 = l2; } while (l1!=NULL) { if(!ifadd) { if(flag) { flag = false; ans = ans_head = ans; } else { ans->next = new ListNode((l1->val)%10); ans = ans->next; } if((l1->val)/10==1) ifadd = true; l1 = l1->next; } else { if(flag) { flag = false; ans = new ListNode((l1->val+1)%10); ans_head = ans; } else { ans->next = new ListNode((l1->val+1)%10); ans = ans->next; } if((l1->val+1)/10==0) ifadd = false; l1 = l1->next; } } if (ifadd) { ans->next = new ListNode(1); } else { return ans_head; } return ans_head; }
In this subject, I made two mistakes altogether:
1. When assigning a pointer, assigning a null pointer to the pointer of the object to be created causes the newly instantiated object not to enter the list of chains.
2. Forget to process last carry
If the title is changed to chain table storage from high to low, then a reverse single-chain table is also needed to store the answers from low to high, so that when multiple rounds occur, the answers are rounded up one by one.