java实现两个链表的数相加,计算两个单链表所代表的数之和——链表相加法

题目:给定两个单链表,链表的每个结点代表一位数,计算两个数的和。例如:输入链表(3->1->5)和链表(5->9->2),输出8->0->8,即513+295=808.

注意:个位数在链第一个元素。

python3实现该算法,具体如下代码。

#!/usr/bin/python3

class LNode:

"""

链表结点

"""

def __init__(self,x=0):

self.data=x

self.next=None

def genLinkedList(dataList=None):

"""

将List类型转换为链表;

"""

listHead=LNode()

p=listHead

if dataList is not None:

for x in dataList:

nd=LNode(x)

p.next=nd

p=nd

return listHead

def toList(hp):

"""

将链表转换为List

"""

if hp is None or hp.next is None:

return []

lst=[]

p=hp.next

while p is not None:

lst.append(p.data)

p=p.next

return lst

def add(h1,h2):

"""

计算单链表之和;

c表示进位。

"""

if h1 is None or h1.next is None:

return h2

if h2 is None or h2.next is None:

return h1

p1=h1.next

p2=h2.next

tmp=None

c=0

resultHead=LNode()

p=resultHead

while p1 is not None and p2 is not None:

tmp=LNode()

sums=p1.data+p2.data+c

tmp.data=sums%10

#c=round(sums/10)

c=1 if sums >=10 else 0

p.next=tmp

p=tmp

p1=p1.next

p2=p2.next

px=p1 if p2 is None else p2

while px is not None:

tmp=LNode()

sums=px.data+c

tmp.data=sums%10

c=1 if sums >=10 else 0

p.next=tmp

p=tmp

px=px.next

if c==1:

tmp=LNode(c)

p.next=tmp

p=tmp

return resultHead

if __name__=='__main__':

list1=[3,4,5,6,7,8]

list2=[9,8,7,6,5]

hp1=genLinkedList(list1)

hp2=genLinkedList(list2)

print('List 1st:{L1}'.format(L1=list1))

print('List 1st:{L2}'.format(L2=list2))

res=add(hp1,hp2)

print('List Ret:{LRes}'.format(LRes=toList(res)))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用Python语言实现的代码: ```python class Node: def __init__(self, coef, exp, next=None): self.coef = coef self.exp = exp self.next = next class Polynomial: def __init__(self): self.head = Node(None, None) self.current = self.head def add_term(self, coef, exp): self.current.next = Node(coef, exp) self.current = self.current.next def __add__(self, other): result = Polynomial() cur1 = self.head.next cur2 = other.head.next while cur1 and cur2: if cur1.exp > cur2.exp: result.add_term(cur1.coef, cur1.exp) cur1 = cur1.next elif cur1.exp < cur2.exp: result.add_term(cur2.coef, cur2.exp) cur2 = cur2.next else: coef_sum = cur1.coef + cur2.coef if coef_sum != 0: result.add_term(coef_sum, cur1.exp) cur1 = cur1.next cur2 = cur2.next while cur1: result.add_term(cur1.coef, cur1.exp) cur1 = cur1.next while cur2: result.add_term(cur2.coef, cur2.exp) cur2 = cur2.next return result def __str__(self): if not self.head.next: return '0' result = '' cur = self.head.next while cur: if cur.coef > 0 and len(result) > 0: result += '+' if cur.coef != 1 or cur.exp == 0: result += str(cur.coef) if cur.exp > 0: result += 'x' if cur.exp > 1: result += '^' + str(cur.exp) cur = cur.next return result p1 = Polynomial() p1.add_term(3, 4) p1.add_term(2, 3) p1.add_term(-5, 2) p1.add_term(7, 0) p2 = Polynomial() p2.add_term(-2, 3) p2.add_term(4, 2) p2.add_term(1, 1) p2.add_term(3, 0) p3 = p1 + p2 print(p1, '+', p2, '=', p3) ``` 在这个程序中,每个多项式都是一个带头结点的有序链表,其中每个节点代表一个多项式的单项式。我们可以通过add_term()方链表中添加单项式,然后通过运符重载的方式实现多项式的加。 对于加,我们首先需要遍历两个链表,将指较小的单项式添加到结果链表中;如果两个单项式的指相等,我们需要将它们的系相加,然后将相加的结果加入到结果链表中。最后,我们需要将两个链表中剩余的单项式全部添加到结果链表中,并返回结果链表对应的多项式。 为了方便输出,我们还定义了__str__()方,将多项式转换成字符串,方便输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值