Add Two Numbers Solutions in Python
Number 2
Difficulty Medium
Acceptance 33.9%
Link LeetCode
Solutions
Python solution by pezy/LeetCode
#!python3# Definition for singly-linked list.class ListNode:def __init__(self, x):self.val = xself.next = Noneclass Solution:def addTwoNumbers(self, l1, l2):""":type l1: ListNode:type l2: ListNode:rtype: ListNode"""head = ListNode(0)p = headquot = 0while l1 or l2 or quot != 0:if l1:quot += l1.vall1 = l1.nextif l2:quot += l2.vall2 = l2.nextquot, rem = divmod(quot, 10)p.next = ListNode(rem)p = p.nextreturn head.nextdef compareLinkedList(l1, l2):while l1 or l2:if not (l1 and l2) or l1.val != l2.val:return Falsel1 = l1.nextl2 = l2.nextreturn Trueif __name__ == "__main__":l1 = ListNode(2)l1.next = ListNode(4)l1.next.next = ListNode(3)l2 = ListNode(5)l2.next = ListNode(6)l2.next.next = ListNode(4)lsum = ListNode(7)lsum.next = ListNode(0)lsum.next.next = ListNode(8)print(compareLinkedList(Solution().addTwoNumbers(l1, l2), lsum))