[title] assuming that the value of each node is between 0 and 9, the whole linked list can represent an integer. For example, 9 - > 3 - > 7 can represent an integer 937. Given the header nodes head1 and head2 of the two linked lists, please generate a result representing the added value of the two integers
[example]
[note] first find the natural number represented by the two linked lists, then find the sum of the two integers, and finally convert the sum into a linked list. Because the number represented by the linked list is very large, this method may cause overflow, which is not recommended
[problem solving ideas]
(1) Using stack structure
- Traverse the two linked lists from left to right, and press the values on the stack during the traversal, so as to generate the reverse order stack of the two linked lists, expressed as S1 and S2
- Pop up s1 and s2 synchronously, which is equivalent to the pop-up of two linked lists from low to high. In this process, an additive linked list can be generated. If there is carry, it is represented by ca
- When s1 and s2 are empty, you need to pay attention to whether the carry information is 1. If it is 1, you need to generate a new node with a node value of 1.
- Returns the generated linked list
(2) Using the reverse order of the linked list
- First, reverse the two linked lists, so that you can get the numbers from low to high, thus saving stack space
- Then, the two linked lists are traversed synchronously, and the carry is represented by ca
- After both linked lists are traversed, you also need to pay attention to whether the carry information is 1. If it is 1, you need to generate a new node with a node value of 1.
- Put the two linked lists back in reverse order
- Returns the generated linked list
[C++]
(1) Using stack structure
ListNode* addList1(ListNode* head1,ListNode* head2){ stack<int> s1; stack<int> s2; while(head1 != NULL){ s1.push(head1->value); head1 = head1->next; } while(head2 != NULL){ s2.push(head2->value); head2 = head2->next; } int ca = 0;//Record carry information int n1 = 0;//Record the value in s1 int n2 = 0;//s2 int n = 0; ListNode* head = NULL; ListNode* pre = NULL; while(!s1.empty() || !s2.empty()){ if(!s1.empty()){ n1 = s1.top(); s1.pop(); }else{ n1 = 0; } if(!s2.empty()){ n2 = s2.top(); s2.pop(); }else{ n2 = 0; } n = n1 + n2 + ca; //Generate additive linked list pre = head; head = new ListNode(n % 10); head->next = pre; ca = n / 10; } if(ca == 1){ pre = head; head = new ListNode(1); head->next = pre; } return head; }
(2) Using reverse linked list
//Reverse linked list ListNode* reverseList(ListNode* head){ ListNode* cur = head; ListNode* pre = NULL; ListNode* next = NULL; while(cur != NULL){ next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } //Generate additive linked list ListNode* addList2(ListNode* head1,ListNode* head2){ head1 = reverseList(head1); head2 = reverseList(head2); int n1 = 0; int n2 = 0; int n = 0; int ca = 0; ListNode* l1 = head1; ListNode* l2 = head2; ListNode* head = NULL; ListNode* pre = NULL; while(l1 != NULL || l2 != NULL){ n1 = l1 != NULL ? l1->value : 0; n2 = l2 != NULL ? l2->value : 0; n = n1 + n2 + ca; pre = head; head = new ListNode(n % 10); head->next = pre; ca = n / 10; l1 = l1 != NULL ? l1->next : NULL; l2 = l2 != NULL ? l2->next : NULL; } if(ca == 1){ pre = head; head = new ListNode(1); head->next = pre; } reverseList(head1); reverseList(head2); return head; }