链表求和

来源:互联网 发布:鼻子变小知乎 编辑:程序博客网 时间:2024/05/28 06:08

你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。

样例

给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null





/** * Definition of TreeNode: * class TreeNode { * public: *     int val; *     TreeNode *left, *right; *     TreeNode(int val) { *         this->val = val; *         this->left = this->right = NULL; *     } * } */class Solution {    /**     * @param root: The root of binary tree.     * @return: Inorder in vector which contains node values.     */public:    vector<int> inorderTraversal(TreeNode *root) {        // write your code here        vector <int> result;        if(root==NULL){            return result;        }        inorderTraversal(root->left);        result.push_back(root->val);        inorderTraversal(root->right);        return result;    }};


1 0