添加两个大的数字再次psented链表$ P $没有扭转链表 [英] Add two big numbers represented as linked lists without reversing the linked lists

查看:188
本文介绍了添加两个大的数字再次psented链表$ P $没有扭转链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有2个大数字再psented链表,你怎么添加它们,并把结果存储在一个单独的链接列表$ P $。 例如:

Suppose you have 2 big numbers represented as linked lists, how do you add them and store the result in a separate linked list. eg

a = 2 -> 1 -> 7 
b = 3 -> 4
result = 2 -> 5 -> 1

您可以添加他们没有扭转链表

Can you add them without reversing the linked lists

推荐答案

伪code:
步骤1.遍历链表和推动元件在两个不同的堆叠
第2步:从弹出顶部元素都堆叠
第3步:添加的元素(+从previous添加任何进位)和存储进一个临时变量
步骤4.创建与和某个节点,然后将其插入结果列表
开头

Pseudocode:
Step 1. Traverse the linked lists and push the elements in two different stacks
Step 2. Pop the top elements from both the stacks
Step 3. Add the elements (+ any carry from previous additions) and store the carry in a temp variable
Step 4. Create a node with the sum and insert it into beginning of the result list

这篇关于添加两个大的数字再次psented链表$ P $没有扭转链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆