程序员面试金典(6)两个链表逆向表示的整数求和

来源:互联网 发布:淘宝买dnf账号 编辑:程序博客网 时间:2024/05/16 04:39
#include <iostream>
#include <string>
#include <map>
using namespace std;

struct Node
{
    int data;
    Node* next;
};

Node* addLists(Node* n1, Node* n2, int carry)
{
    if (!n1 && !n2 && !carry)
    {
        return NULL;
    }
    Node* res;
    int value = carry;
    if (!n1)
    {
        value += n1->data;
    }
    if (!n2)
    {
        value += n2->data;
    }
    res->data = value % 10;
    Node* more = addLists(n1 == NULL ? NULL : n1->next, n2 == NULL ? NULL : n2->next, value >= 10 ? 1 : 0);
    res->next = more;
    return res;    
}

int main()
{
    
    //getchar();
    return 0;
}

0 0
原创粉丝点击