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
Add each element of two list with the help of a variable "carry". When both of the lists meet their end, return result.Output: 7 -> 0 -> 8
The key point is to check the carry before return, if carry = 1 the highest bit should be 1, that means add a new node with value "1" to the result list.
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below // DO NOT write int main() function int carry=0; ListNode* res=new ListNode(0); ListNode* head = res; while (l1 && l2){ res->next=new ListNode((l1->val+l2->val+carry)%10); carry = (l1->val+l2->val+carry)/10; l1=l1->next; l2=l2->next; res=res->next; } while (l1){ res->next=new ListNode((l1->val+carry)%10); carry = (l1->val+carry)/10; l1=l1->next; res=res->next; } while (l2){ res->next=new ListNode((l2->val+carry)%10); carry = (l2->val+carry)/10; l2=l2->next; res=res->next; } if (carry>0){ res->next = new ListNode(carry); } return head->next; }
No comments:
Post a Comment