Adding two numbers Leetcode 445. II ---- python

1. Title Description

Given two non-empty list to represent two non-negative integers. The highest number of bits are located in the list starting position. They each node only stores a single digit. The two numbers together will return a new list.

You may assume that in addition to the numbers 0, these two figures will not begin with a zero.
Example :
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

2. Problem-solving ideas

If this question is set against the initial list, the list can be stored with the stack to the value, and then adding, then adding the present value of the new list.
Step:
(1) with two stacks (stack1, stack2) are able to save the values of two lists
(2) Remove the two elements from the stack, added after the addition on the new stack (stack3) of
(3 ) the new element into the list of the stack

3. code implementation

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        stack1 = []
        stack2 = []
        stack3 = []
        # 将两链表的元素分别入栈
        while(l1):
            stack1.append(l1.val)
            l1 = l1.next
        while(l2):
            stack2.append(l2.val)
            l2 = l2.next
        lend = 0

        # 将两链表的元素相加后,入栈
        while(stack1 and stack2):
            temp = stack1.pop(len(stack1)-1) + stack2.pop(len(stack2)-1)+ lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        while(stack1 == [] and  stack2 != []):
            temp = stack2.pop(len(stack2)-1)+ lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        while(stack2 == [] and  stack1 != []):
            temp = stack1.pop(len(stack1)-1) + lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        if(lend == 1):
            stack3.append(1)

        #将栈的值放入链表
        dummy = ListNode(None)
        head = dummy
        for i in stack3[::-1]:
            node = ListNode(i)
            head.next = node
            head = head.next
        return dummy.next

Here Insert Picture Description

4. The test and test results

Test Case

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        stack1 = []
        stack2 = []
        stack3 = []
        # 将两链表的元素分别入栈
        while(l1):
            stack1.append(l1.val)
            l1 = l1.next
        while(l2):
            stack2.append(l2.val)
            l2 = l2.next
        lend = 0

        # 将两链表的元素相加后,入栈
        while(stack1 and stack2):
            temp = stack1.pop(len(stack1)-1) + stack2.pop(len(stack2)-1)+ lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        while(stack1 == [] and  stack2 != []):
            temp = stack2.pop(len(stack2)-1)+ lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        while(stack2 == [] and  stack1 != []):
            temp = stack1.pop(len(stack1)-1) + lend
            if(temp < 10):
                stack3.append(temp)
                lend = 0
            else:
                stack3.append(int(str(temp)[1]))
                lend = 1

        if(lend == 1):
            stack3.append(1)

        #将栈的值放入链表(新开辟的链表空间)
        dummy = ListNode(None)
        head = dummy
        for i in stack3[::-1]:
            node = ListNode(i)
            head.next = node
            head = head.next
        return dummy.next

    #打印链表操作
    def printLink(self,root):
        r = root
        while(r):
            print(r.val)
            r = r.next

root1 = ListNode(9)
n2 = ListNode(4)
n3 = ListNode(6)
root1.next = n2
n2.next = n3

root2 = ListNode(0)
# r2 = ListNode(5)
# r3 = ListNode(7)
# root2.next = r2
# r2.next = r3

s = Solution()
result= s.addTwoNumbers(root1,root2)
s.printLink(result)

Test Results:

9
4
6

Guess you like

Origin blog.csdn.net/u013075024/article/details/92570876