美文网首页
1.3 计算两个单链表所代表的的数之和

1.3 计算两个单链表所代表的的数之和

作者: 就是果味熊 | 来源:发表于2020-02-23 16:16 被阅读0次

    题目

    给定两个单链表,链表的每个结点代表一位数,计算像个数的和,例如:
    3>1>5 和 5>9>2 输出 8>0>8,注意个位数在链表头。

    #%%
    class LHead:
        def __init__(self):
            self.next = None
        
        def get_ordered_list(self): # 以列表形式打印顺序表
            order_list = []
            if self.next == None or self == None:
                return order_list
            else:
                cur = self.next
                while cur != None:
                    order_list.append(cur.data)
                    cur = cur.next
                return order_list
            
        def ordered_list_size(self): #获得链表长度
            
            return len(self.get_ordered_list())
        
        def create_ordered_list(self,size): #在新建头结点后面生成有序链表
            i = 0
            tmp = None
            cur = self
            ordered_list = []
             #构造单链表
            while i < size:
                 tmp = LNode(i)
                 tmp.next = None
                 cur.next = tmp
                 cur = tmp
                 i += 1
            cur = self.next
            while cur != None:
                ordered_list.append(cur.data)
                cur = cur.next
            return ordered_list,self
        
    #%%
    class LNode:
        def __init__(self,data):
            self.data = data
            self.next = None
    
    
    #%%
    # 整数相加法,缺点是当数字较大时(超出了long的表示范围,就无法使用这种方法)
    def head_to_num1(head):
        if head == None or head.next == None:
            return 0
        else:
            list1 = []
            num = 0
            cur = head.next
            while cur != None:
                list1.append(cur.data)
                cur = cur.next
            list1.reverse()
            for i in list1:
                num = str(num) + str(i)
            num = int(num)
        return num
    
    
    def head_to_num2(head):
        if head == None or head.next == None:
            return 0
        else:
            list1 = []
            num = 0
            cur = head.next
            while cur != None:
                list1.append(cur.data)
                cur = cur.next
            list1.reverse()
            for idx, value in enumerate(list1):
                num = num + value * 10 **(len(list1) - idx - 1)
        return num
    
    def head_num_add(head1,head2):
        num_list = []
        head = LHead()
        cur = head
        num1 = head_to_num2(head1)
        num2 = head_to_num2(head2) # 使用第二种字符串变为数字的函数
        nums = num1 + num2
        nums = str(nums)
        for i in nums:
            num_list.append(int(i))
        num_list.reverse()
        for i in num_list:
            a = LNode(i)
            cur.next = a
            cur = cur.next
        return head
            
    #%%
    # 链表相加法
    def head_add_LHead(head1,head2):
        if head1 == None or head1.next == None:
            return head2
        if head2 == None or head2.next == None:
            return head1
        
        plus = 0
        cur1 = head1.next
        cur2 = head2.next
        sum_head = LHead()
        sum_cur = sum_head 
        while cur1 != None and cur2 != None:
            sums = cur1.data + cur2.data + plus
            if sums > 9:
                plus = 1
                sums = sums - 10
            else:
                plus = 0
            a = LNode(sums)
            sum_cur.next = a
            sum_cur = sum_cur.next
            cur1 = cur1.next
            cur2 = cur2.next
        if cur1:
            cur = cur1
        else:
            cur = cur2
        while cur != None:
            sums = cur.data + plus
            if sums > 9:
                plus = 1
                sums = sums - 10
            else:
                plus = 0
            b = LNode(sums)
            sum_cur.next = b
            sum_cur = sum_cur.next
            cur = cur.next
        if plus == 1:
            sum_cur.next = LNode(1)
        return sum_head
        ```

    相关文章

      网友评论

          本文标题:1.3 计算两个单链表所代表的的数之和

          本文链接:https://www.haomeiwen.com/subject/beijqhtx.html