[LeetCode] Add Two Numbers 两数相加 C++完整代码实现

在这里由于两数相加用到的链表的成员函数比较少,没有单独实现链表的头文件和源文件。

单链表节点定义的头文件

#ifndef NODE_H
#define NODE_H


class Node
{
public:
    Node(int data = 0);
    int m_iVal;
    Node* next;
    void printNode();
    void NodeTraverse();
    bool NodeInsertTail(Node* pNode);
};

#endif
 

 

单链表节点成员函数实现的源文件

#include "pch.h"
#include <iostream>
#include "Node.h"

using namespace std;

Node::Node(int data)
{
    m_iVal = data;
}

void Node::printNode()
{
    cout << m_iVal << endl;
}

void Node::NodeTraverse()
{
    Node* currentNode = this;
    while (currentNode->next != nullptr)
    {
        currentNode = currentNode->next;
        currentNode->printNode();
    }
}

bool Node::NodeInsertTail(Node* pNode)
{
    Node* currentNode = this;
    while (currentNode->next != nullptr)
    {
        currentNode = currentNode->next;
    }
    Node* newNode = new Node;
    if (newNode == nullptr)
    {
        return false;
    }
    newNode->m_iVal = pNode->m_iVal;
    newNode->next = nullptr;
    currentNode->next = newNode;
    return true;
}

 

// Add_Two_Num.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include "Node.h"

using namespace std;


Node* addTwoNumbers(Node* node1, Node* node2)
{
    Node* p1 = node1;
    Node* p2 = node2;
    Node* nodeHead = new Node(-1);
    Node* cur = nodeHead;
    int carried = 0;
    while (p1 || p2)
    {

        //在两个单独链表的相同位置节点上进行整除取余的操作,
        int a = p1 ? p1->m_iVal : 0;
        int b = p2 ? p2->m_iVal : 0;
        cur->next = new Node((a + b + carried) % 10);
        carried = (a + b + carried) / 10;

        //cur指向新生成的一个节点,

        cur = cur->next;

        // 对两个单独链表分别读取当前节点所指向的下一个相同位置的节点
        p1 = p1 ? p1->next : nullptr;
        p2 = p2 ? p2->next : nullptr;
    }
    cur->next = carried ? new Node(1) : nullptr;
    Node* ret = nodeHead->next;
    delete nodeHead;
    nodeHead = nullptr;
    return ret;
}


int main()
{

    Node node1;
    node1.m_iVal = 2;
    Node node2;
    node2.m_iVal = 4;
    Node node3;
    node3.m_iVal = 3;
    Node node4;
    node4.m_iVal = 5;
    Node node5;
    node5.m_iVal = 6;
    Node node6;
    node6.m_iVal = 4;

    Node* headNode1 = new Node(0);
    Node* headNode2 = new Node(0);
    Node* headNode3 = new Node(0);

    headNode1->NodeInsertTail(&node1);
    headNode1->NodeInsertTail(&node2);
    headNode1->NodeInsertTail(&node3);

    headNode1->NodeTraverse();

    headNode2->NodeInsertTail(&node4);
    headNode2->NodeInsertTail(&node5);
    headNode2->NodeInsertTail(&node6);

    headNode2->NodeTraverse();

    headNode3 = addTwoNumbers(headNode1, headNode2);

    headNode3->NodeTraverse();

    delete headNode1;
    headNode1 = nullptr;
    delete headNode2;
    headNode2 = nullptr;
    delete headNode3;
    headNode3 = nullptr;

    std::cout << "Hello World!\n";
}
 

```c /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode* head = NULL; struct ListNode* tail = NULL; int carry = 0; while(l1 != NULL || l2 != NULL){ int n1 = (l1 != NULL) ? l1->val : 0; int n2 = (l2 != NULL) ? l2->val : 0; int sum = n1 + n2 + carry; carry = sum / 10; struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = sum % 10; newNode->next = NULL; if(head == NULL){ head = newNode; tail = newNode; }else{ tail->next = newNode; tail = newNode; } if(l1 != NULL){ l1 = l1->next; } if(l2 != NULL){ l2 = l2->next; } } if(carry > 0){ struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = carry; newNode->next = NULL; tail->next = newNode; tail = newNode; } return head; } ``` 这是leetcode题目“两数相加”的完整C语言解法。首先定义了一个结构体`ListNode`来表示单链表的结点。然后,我们使用头尾指针`head`和`tail`来创建新的结果链表。我们还定义了一个变量`carry`来保存进位值。 在循环中,我们分别取出两个链表当前结点的值,并与进位值相加。如果链表的长度不一致,我们使用0来代替较短链表的缺失结点值。然后,我们将求和结果的个位数作为新结点的值,并更新进位值。 接着,我们创建新结点并将其加入结果链表中。第一个新结点既是头指针也是尾指针,而后续新结点都仅作为尾指针的next指向。 最后,如果进位值大于0,我们将其作为新结点加入结果链表的尾部。 最后返回头指针head即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值