📌  相关文章
📜  用链表表示的两个数字相乘的Python程序

📅  最后修改于: 2022-05-13 01:56:56.772000             🧑  作者: Mango

用链表表示的两个数字相乘的Python程序

给定两个由链表表示的数字,编写一个函数,返回这两个链表的乘积。

例子:

Input: 9->4->6
        8->4
Output: 79464

Input: 3->2->1
        1->2
Output: 3852

解决方案
遍历两个列表并生成需要相乘的数字,然后返回两个数字的相乘值。
从链表表示生成数字的算法:

1) Initialize a variable to zero
2) Start traversing the linked list
3) Add the value of the first node to this variable
4) From the second node, multiply the variable by 10
   and also take the modulus of this value by 10^9+7
   and then add the value of the node to this 
   variable.
5) Repeat step 4 until we reach the last node of the list. 

将上述算法与两个链表一起使用以生成数字。

下面是用链表表示的两个数字相乘的程序:

Python3
# Python3 to multiply two numbers
# represented as Linked Lists
    
# Linked list node class 
class Node: 
        
    # Function to initialize the node  
    def __init__(self, data):        
        self.data = data 
        self.next = None 
        
# Linked List Class
class LinkedList:
    
    # Function to initialize the
    # LinkedList class.
    def __init__(self):
    
        # Initialize head as None
        self.head = None
    
    # Function to insert a node at the
    # beginning of the Linked List
    def push(self, new_data): 
        
        # Create a new Node 
        new_node = Node(new_data) 
    
        # Make next of the new Node 
        # as head 
        new_node.next = self.head 
    
        # Move the head to point to 
        # new Node 
        self.head = new_node
            
    # Function to print the Linked 
    # List
    def printList(self):        
        ptr = self.head  
        while (ptr != None):
            print(ptr.data, 
                  end = '')
            if ptr.next != None:
                print('->', 
                      end = '')                
            ptr = ptr.next
                
        print()
    
# Multiply contents of two Linked 
# Lists
def multiplyTwoLists(first, second):  
    num1 = 0
    num2 = 0
  
    first_ptr = first.head
    second_ptr = second.head
      
    while first_ptr != None or second_ptr != None:
        if first_ptr != None:
            num1 = (num1 * 10) + first_ptr.data
            first_ptr = first_ptr.next
      
        if second_ptr != None:
            num2 = (num2 * 10) + second_ptr.data
            second_ptr = second_ptr.next
      
    return num1 * num2
    
# Driver code
if __name__=='__main__':
    
    first = LinkedList()
    second = LinkedList()
    
    # Create first Linked List 9->4->6
    first.push(6)
    first.push(4)
    first.push(9)
  
    # Printing first Linked List
    print("First list is: ", end = '')
    first.printList()
    
    # Create second Linked List 8->4
    second.push(4)
    second.push(8)
  
    # Printing second Linked List
    print("Second List is: ", 
           end = '')
    second.printList()
    
    # Multiply two linked list and
    # print the result
    result = multiplyTwoLists(first, 
                              second)
    print("Result is: ", result)
# This code is contributed by kirtishsurangalikar


输出:

First List is: 9->4->6
Second List is: 8->4
Result is: 79464

有关详细信息,请参阅有关将链接列表表示的两个数字相乘的完整文章!