程序员面试算法宝典-1.3 如何计算两个单链表所代表的数之和

题目描述:

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

"""链表"""
# 定义链表的结点
class LNode():
    def __init__(self):
        self.data = None # 数据域
        self.next = None # 指针域

# 链表相加
def addLink(head1, head2):
    # 判断链表是否为空
    if head1 is None or head1.next is None:
        return head2
    if head2 is None or head2.next is None:
        return head1
    c = 0 # 用来记录进位
    sums=0 # 用来记录两个结点相加的值
    p1 = head1.next # 用来遍历h1
    p2 = head2.next # 用来遍历h2
    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 = sums // 10 # 进位
        p.next = tmp
        p = tmp
        p1 = p1.next
        p2 = p2.next
    if p1 is None:
        while p2 is not None:
            tmp = LNode()
            sums = p2.data + c
            tmp.data = sums % 10
            c = sums // 10
            p.next = tmp
            p = tmp
            p2 = p2.next
    if p2 is None:
        while p1 is not None:
            tmp = LNode()
            sums = p1.data + c
            tmp.data = sums % 10
            c = sums // 10 # 取整
            p.next = tmp
            p = tmp
            p1 = p1.next
    # 如果计算完成后还有进位,则增加新的结点
    if c==1:
        tmp = LNode
        tmp.data = c
        p.next = tmp
    return resultHead

if __name__=="__main__":
    i = 1
    head1 = LNode()
    head2 = LNode()
    cur = head1
    # 构造第一个链表
    while i < 7:
        tmp = LNode()
        tmp.data = i+2
        cur.next = tmp
        cur = tmp
        i += 1
    # 构造第二个链表
    cur = head2
    i = 9
    while i>4:
        tmp = LNode()
        tmp.data = i
        cur.next = tmp
        cur = tmp
        i -= 1
    print("\nHead1:")
    cur = head1.next
    while cur is not None:
        print(cur.data)
        cur = cur.next
    print("\nHead2:")
    cur = head2.next
    while cur is not None:
        print(cur.data)
        cur = cur.next
    # 相加后
    print("\n相加后:")
    cur = addLink(head1, head2)
    cur = cur.next
    while cur is not None:
        print(cur.data)
        cur = cur.next

Result:

Head1:
3
4
5
6
7
8

Head2:
9
8
7
6
5

相加后:
2
3
3
3
3
9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值