leetcode445. 两数相加 II---头插法

头插法:

每次插入节点从头节点插入

尾插法:

每次插入节点从尾节点插入

def addTwoNumbers(self, l1, l2):
        """
        注意
        1.初始化的是头节点,最后的结果要返回头节点的next
        2.节点的插入的时候注意顺序,箭头指向哪个哪个在等号的右边
        主要思想是头插法构建链表
        """
        stack1,stack2,flag = [],[],0
        head = ListNode(0)
        while l1:
            stack1.append(l1.val)
            l1 = l1.next
        while l2:
            stack2.append(l2.val)
            l2 = l2.next
        while stack1 and stack2:
            t = int(stack1.pop() + stack2.pop()+flag)
            ####头插法插入链表
            if t < 10:
                p = ListNode(t)
                p.next = head.next
                head.next = p
                flag = 0
            ####保存大于10的余数
            else:
                a = int(t%10)
                flag = int(t/10)
                p = ListNode(a)
                p.next = head.next
                head.next = p
                    
        while stack1:
            t = int(stack1.pop()) + flag
            if t < 10:
                p = ListNode(t)
                p.next = head.next
                head.next = p
                flag = 0
            else:
                a = int(t%10)
                p = ListNode(a)
                p.next = head.next
                head.next = p
                flag = int(t/10)
                
        while stack2:
            t = int(stack2.pop()) + flag
            if t < 10:
                p = ListNode(t)
                p.next = head.next
                head.next = p
                flag = 0
            else:
                a = int(t%10)
                p = ListNode(a)
                p.next = head.next
                head.next = p
                flag = int(t/10)
        if flag != 0:
            p = ListNode(flag)
            p.next = head.next
            head.next = p
        return head.next

整理代码

def addTwoNumbers(self, l1, l2):
        """
        """
        stack1, stack2, flag = [], [], 0
        head = ListNode(0)
        while l1:
            stack1.append(l1.val)
            l1 = l1.next
        while l2:
            stack2.append(l2.val)
            l2 = l2.next
        while stack1 and stack2:
            t = int(stack1.pop() + stack2.pop() + flag)
            flag = self.insert_head(t,head)

        while stack1:
            t = int(stack1.pop()) + flag
            flag = self.insert_head(t, head)

        while stack2:
            t = int(stack2.pop()) + flag
            flag = self.insert_head(t, head)
        if flag != 0:
            p = ListNode(flag)
            p.next = head.next
            head.next = p
        return head.next
    def insert_head(self,t,head):
        ####头插法插入链表
        if t < 10:
            p = ListNode(t)
            p.next = head.next
            head.next = p
            flag = 0
        ####保存大于10的余数
        else:
            a = int(t % 10)
            flag = int(t / 10)
            p = ListNode(a)
            p.next = head.next
            head.next = p
        return flag

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值