逆序一位数数组求和

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
、、、、、、、、、、、、、、借鉴LeetCode 官方解答,自己写的一直报内存溢出。。。。



ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* temp; temp= new ListNode(0); int numlast = 0; ListNode* ptr1 = l1; ListNode* ptr2 = l2; ListNode* cur = new ListNode(0); ListNode* result = cur; while(ptr1 !=NULL || ptr2 !=NULL) { int x = (ptr1 == NULL)? 0:ptr1->val; int y = (ptr2 == NULL)? 0:ptr2->val; temp->val = x + y + numlast; numlast = temp->val/10; temp->val = temp->val%10; cur->next = new ListNode(temp->val); cur = cur->next; ptr1 = ptr1->next; ptr2 = ptr2->next; } if(numlast == 1) { cur->next = new ListNode(1); } return result->next; }
完整代码
/*
* * Definition for singly-linked list.*/ // struct ListNode{ // int val; // ListNode *next; // ListNode(int x) : val(x), next(NULL) {} // }; class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* temp; temp= new ListNode(0); int numlast = 0; ListNode* ptr1 = l1; ListNode* ptr2 = l2; ListNode* cur = new ListNode(0); ListNode* result = cur; while(ptr1 !=NULL || ptr2 !=NULL) { int x = (ptr1 == NULL)? 0:ptr1->val; int y = (ptr2 == NULL)? 0:ptr2->val; temp->val = x + y + numlast; numlast = temp->val/10; temp->val = temp->val%10; cur->next = new ListNode(temp->val); cur = cur->next; ptr1 = ptr1->next; ptr2 = ptr2->next; } if(numlast == 1) { cur->next = new ListNode(1); } return result->next; } }; void trimLeftTrailingSpaces(string &input) { input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) { return !isspace(ch); })); } void trimRightTrailingSpaces(string &input) { input.erase(find_if(input.rbegin(), input.rend(), [](int ch) { return !isspace(ch); }).base(), input.end()); } vector<int> stringToIntegerVector(string input) { vector<int> output; trimLeftTrailingSpaces(input); trimRightTrailingSpaces(input); input = input.substr(1, input.length() - 2); stringstream ss; ss.str(input); string item; char delim = ','; while (getline(ss, item, delim)) { output.push_back(stoi(item)); } return output; } ListNode* stringToListNode(string input) { // Generate list from the input vector<int> list = stringToIntegerVector(input); // Now convert that list into linked list ListNode* dummyRoot = new ListNode(0); ListNode* ptr = dummyRoot; for(int item : list) { ptr->next = new ListNode(item); ptr = ptr->next; } ptr = dummyRoot->next; delete dummyRoot; return ptr; } string listNodeToString(ListNode* node) { if (node == nullptr) { return "[]"; } string result; while (node) { result += to_string(node->val) + ", "; node = node->next; } return "[" + result.substr(0, result.length() - 2) + "]"; } int main() { string line; while (getline(cin, line)) { ListNode* l1 = stringToListNode(line); getline(cin, line); ListNode* l2 = stringToListNode(line); ListNode* ret = Solution().addTwoNumbers(l1, l2); string out = listNodeToString(ret); cout << out << endl; } return 0; }
原文地址:https://www.cnblogs.com/8335IT/p/10397544.html