Python程序员面试算法宝典---解题总结: 第1章 链表 1.3 如何计算两个单链表所代表的数之和

#!/usr/bin/env python
# encoding: utf-8

'''
Python程序员面试算法宝典---解题总结: 第1章 链表 1.3 如何计算两个单链表所代表的数之和

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

分析:
问题的关键就是逆序的问题,最高位在最后面,还有一个问题就是链表可能长度不一样:
例如
3->1->5 和 5->9
表示的是: 513+95=608
最简单的方式是: 遍历链表,直接累加。
第一位是个位:
3*1 + 1*10 + 5 * 100 = 513
设定:
scale=1
sum=0
sum += currValue * scale
scale *= 10
陷阱: 如果链表很长,可能导致溢出。
最好的方式就是直接计算出结果

最简单的方式:
输出的时候再构建一个链表

关键:
1 陷阱
链表很长,如果各位累加计算可能造成溢出,
最好就是逐位累加,只保留当前位的值,和进位的值。


参考:
Python程序员面试算法宝典
'''

class Node(object):
    def __init__(self, data=None, nextNode=None):
        self.data = data
        self.nextNode = nextNode


def buildList(head, arr):
    if not head or not arr:
        return head
    currNode = head
    for data in arr:
        newNode = Node(data)
        currNode.nextNode = newNode
        currNode = newNode
    return head


def printList(head):
    if not head:
        return
    currNode = head.nextNode
    while currNode:
        data = currNode.data
        print data
        currNode = currNode.nextNode

def sumList(head):
    if not head:
        return 0
    if head.nextNode is None:
        return 0
    currNode = head.nextNode
    scale = 1
    sum = 0
    while currNode:
        data = currNode.data
        sum += scale * data
        scale *= 10
        currNode = currNode.nextNode
    return sum


def sumTwoList(head1, head2, resultHead):
    if head1 is None and head2 is None:
        return resultHead
    elif head1 is None:
      return head2
    elif head2 is None:
        return head1
    currNode1 = head1.nextNode
    currNode2 = head2.nextNode
    currNode = resultHead
    overlowValue = 0
    while currNode1 or currNode2:
        value1 = currNode1.data if currNode1 else 0
        value2 = currNode2.data if currNode2 else 0
        passValue = (value1 + value2 + overlowValue) / 10
        data = (value1 + value2 + overlowValue) % 10
        overlowValue = passValue
        newNode = Node(data)
        currNode.nextNode = newNode
        currNode = newNode
        currNode1 = currNode1.nextNode if currNode1 else None
        currNode2 = currNode2.nextNode if currNode2 else None
    if overlowValue != 0:
        newNode = Node(overlowValue)
        currNode.nextNode = newNode
    return resultHead


def process():
    arr1 = [3, 1, 5]
    arr2 = [5, 9, 2]
    arr3 = [5, 9]
    head1 = Node()
    head2 = Node()
    head3 = Node()
    head1 = buildList(head1, arr1)
    head2 = buildList(head2, arr2)
    head3 = buildList(head3, arr3)
    resultHead = Node()
    result = sumTwoList(head1, head2, resultHead)
    printList(result)
    arr4 = [i for i in range(3, 9)]
    arr5 = [i for i in range(9, 4, -1)]
    # print arr4
    # print arr5
    head4 = Node()
    head5 = Node()
    result = Node()
    head4 = buildList(head4, arr4)
    head5 = buildList(head5, arr5)
    result = sumTwoList(head4, head5, result)
    printList(result)
    # result = sumTwoList(head1, head3, resultHead)
    # printList(result)
    # printList(head1)
    # printList(head2)
    # printList(head3)
    # sum1 = sumList(head1)
    # sum2 = sumList(head2)
    # sum3 = sumList(head3)
    # print sum1
    # print sum2
    # print sum3
    # assert sum1 + sum2 == 808
    # assert sum1 + sum3 == 608


if __name__ == "__main__":
    process()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值