力扣 445 两数相加

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

这两个数字都不会以零开头。
示例
输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7
因此本题的实现是用栈来实现的
c++中虽然提供了栈的直接调用,但是本题还是从最基础的栈实现,链表实现来编码的

#include <iostream>
using namespace std;
#define MaxSize 50
typedef struct LNode
{
    int data;
    struct LNode* next;
}LinkNode;
typedef struct SqStack
{
    int data[MaxSize];
    int top;
};
//栈的初始化
void Init_Stack(SqStack *&S)
{
    S = (SqStack*)malloc(sizeof(SqStack));
    S->top = -1;
}
//进栈操作
void Push(SqStack*&S,int& e )
{
    if (S->top == MaxSize - 1)//
        return;
    S->top++;
    S->data[S->top] = e;
}
//出栈操作
int Pop(SqStack* &S, int &e)
{
    if (S->top == - 1)//
        return 0;
    e = S->data[S->top];
    S->top--;
    return e;
}
//初始化链表
void InitList(LinkNode*& L)
{
    L = (LinkNode*)malloc(sizeof(LinkNode));
    L->next = NULL;
}
//尾插法插入元素 为了让插入的元素正序
void Insert_R(LinkNode*& L, int a[], int n)
{
    LinkNode* R,* S;
    int i;
    L = (LinkNode*)malloc(sizeof(LinkNode));
    R= L;
    for (i = 0; i < n; i++)
    {
        S = (LinkNode*)malloc(sizeof(LinkNode));
        S->data = a[i];
        R->next = S;
        R = S;
    }
    R->next = NULL;
}
//头插法
void CreateListF(LinkNode*& L, int a[], int n)
{
    LinkNode* s;
    int i;
    L = (LinkNode*)malloc(sizeof(LinkNode));
    L->next = NULL;
    for (i = 0; i < n; i++)
    {
        s = (LinkNode*)malloc(sizeof(LinkNode));
        s->data = a[i];
        s->next = L->next; //s指向L的next
        L->next = s;
    }
}

//输出线性表
void DispList(LinkNode* L)
{
    LinkNode* s = L->next;
    while (s != NULL)
    {
            cout << s->data << "->";          
            s = s->next;
    }
}
int main()
{
    LinkNode* L1, * L2,*L3;
    SqStack* S1, * S2;
    int flag=0;
    int e,e1, e2;//用来接收栈弹出的元素
    int a[50] = {9,2,4,3};
    int b[50] = {8,6,4};
    int c[7] = {0};//用来为最后返回的链表传参
    int j=0;//让数组下标增长
    int count=0;//用来记录,链表有多少个数字
    int size;//记录c数组的长度变化
    Insert_R(L1, a, sizeof(a)/sizeof(a[0]));
    Insert_R(L2, b, sizeof(b) / sizeof(b[0]));
    Init_Stack(S1); //初始化栈  
    Init_Stack(S2);   
    do
    {
            Push(S1, L1->next->data);
            L1 = L1->next;               
    } while (L1->next->data != NULL);
    do
    {
        Push(S2, L2->next->data);
        L2 = L2->next;
    } while (L2->next->data != NULL);
    while (S1->top !=-1||S2->top !=-1)
    {
       e1= Pop(S1,e);
       flag += e1;
       e2=Pop(S2, e);
       flag += e2;
       c[j++] = flag % 10;
       flag = flag / 10;
    }
    if (flag == 1)//判断最后有没有进位
        c[j] = 1;
    CreateListF(L3, c, j + 1);//此时记录的元素是具体的元素,数组的最大下标得让j+1
    DispList(L3);
    return 0;
}

具体了解更简便的方法阅读下文
java中调用栈机制

力扣两数相加问题的描述为:给两个非空的链表,表示两个非负的整数,它们每位数字按照逆序的方式存储,且每个节点只能存储一位数字,需将两个数相加,并以相同形式返回一个表示和的链表,除数字 0 之外,这两个数都不会以 0 开头[^3]。 ### 解题思路 一种思路是调用函数`addTwoNumbers(l1, l2)`,传入两个链表,分别计算每个链表对应的数值(如`2->3->1`对应数值是 342),之后求出两个数值的和`sum`,最后通过`while`循环对`sum`取余和除 10 取整的操作再把各个位的数添加到一个链表中,最后返回链表[^2]。 ### 代码实现(Python 示例) ```python # 定义链表节点类 class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode(0) current = dummy carry = 0 while l1 or l2 or carry: x = l1.val if l1 else 0 y = l2.val if l2 else 0 # 计算当前位的和以及进位 total = x + y + carry carry = total // 10 digit = total % 10 # 创建新节点并连接到结果链表 current.next = ListNode(digit) current = current.next # 移动到下一个节点 if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next # 辅助函数:将列表转换为链表 def list_to_linked_list(lst): dummy = ListNode(0) current = dummy for val in lst: current.next = ListNode(val) current = current.next return dummy.next # 辅助函数:将链表转换为列表 def linked_list_to_list(head): result = [] current = head while current: result.append(current.val) current = current.next return result # 测试示例 l1 = list_to_linked_list([2, 4, 3]) l2 = list_to_linked_list([5, 6, 4]) result = addTwoNumbers(l1, l2) print(linked_list_to_list(result)) # 输出: [7, 0, 8] ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨里的无聊人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值