leetcode 力扣 002 两数相加 python

  • 创建一个哑结点dummy作为结果链表的头部,用current指针来追踪当前操作的节点。
  • 使用carry变量来追踪进位。
  • 迭代直到l1l2都为空,并且没有进位。
  • 在每个迭代中,计算当前位置的值,更新进位,并将结果添加到结果链表中。
  • 返回dummy.next作为最终的结果链表,跳过哑结点
  • class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            # 创建一个哑结点作为结果链表的头部
            dummy = ListNode(0)
            current = dummy
            carry = 0
            
            while l1 or l2 or carry:
                # 获取当前节点的值,如果节点为空则取0
                val1 = l1.val if l1 else 0
                val2 = l2.val if l2 else 0
                # 计算当前位置的总和(包括进位)
                total = val1 + val2 + carry
                
                # 计算当前位置的进位和实际值
                carry = total // 10
                current.next = ListNode(total % 10)
                
                # 移动到下一个节点
                current = current.next
                l1 = l1.next if l1 else None
                l2 = l2.next if l2 else None
            
            return dummy.next
    
    class ListNode:
        def __init__(self, val=0, next=None):
            self.val = val
            self.next = next
    
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            dummy = ListNode(0)
            current = dummy
            carry = 0
            
            while l1 or l2 or carry:
                val1 = l1.val if l1 else 0
                val2 = l2.val if l2 else 0
                total = val1 + val2 + carry
                
                carry = total // 10
                current.next = ListNode(total % 10)
                
                current = current.next
                l1 = l1.next if l1 else None
                l2 = l2.next if l2 else None
            
            return dummy.next
    
    def print_linked_list(head: ListNode):
        values = []
        while head:
            values.append(head.val)
            head = head.next
        print(" -> ".join(map(str, values)))
    
    def create_linked_list(numbers):
        if not numbers:
            return None
        head = ListNode(numbers[0])
        current = head
        for number in numbers[1:]:
            current.next = ListNode(number)
            current = current.next
        return head
    
    if __name__ == "__main__":
        # 输入链表1
        l1_input = input("Enter the first linked list values separated by spaces: ")
        l1_numbers = list(map(int, l1_input.split()))
        
        # 输入链表2
        l2_input = input("Enter the second linked list values separated by spaces: ")
        l2_numbers = list(map(int, l2_input.split()))
        
        # 创建链表
        l1 = create_linked_list(l1_numbers)
        l2 = create_linked_list(l2_numbers)
        
        # 调用 addTwoNumbers
        sol = Solution()
        result = sol.addTwoNumbers(l1, l2)
        
        # 输出结果
        print("Result Linked List:")
        print_linked_list(result)
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值