2. 两数相加

#include<iostream>
#include<stdio.h>
#include<vector>
#include <algorithm>
#include <functional>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
// 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 *nextL1 = l1->next;////指向下一个结点的指针
		ListNode *nextL2 = l2->next;//指向下一个结点的指针
		ListNode *result = new ListNode(0);//创建新元素
		ListNode *trueResult = result;//最后的结果result指向trueResult,这样可以获取result所接收的全部元素,而result的指针由于每次都往下移,所以每次都更新
		int sum = l1->val + l2->val;//初始化sum,未考虑进位
		int jinWei = 0;//进位初始化未0
		if (sum >= 10)//获得进位值
		{
			jinWei = sum / 10;
			result->val = sum % 10;//获得result的值
		}
		else
		{
			result->val = sum;////获得result的值
			jinWei = 0;
		}

		while (nextL1 != NULL || nextL2 != NULL|| jinWei > 0)
		{
			int nextL1Val = 0;
			int nextL2Val = 0;

			if (nextL1 != NULL)
			{
				nextL1Val = nextL1->val;
				nextL1 = nextL1->next;
			}
			if (nextL2 != NULL){
				nextL2Val = nextL2->val;
				nextL2 = nextL2->next;
			}
			sum = nextL1Val + nextL2Val + jinWei;
			if (sum >= 10)
			{
				jinWei = sum / 10;
				result->next = new ListNode(sum % 10);
			}
			else{
				result->next = new ListNode(sum);
				jinWei = 0;
			}
			result = result->next;
			
		}

		return trueResult;
	}

};
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/277223178dudu/p/10761330.html