|
| 1 | +'''Name : Abhinav kumar |
| 2 | +Github username : Abhinavcode13 |
| 3 | +Repository name : data-structures-and-algorithms |
| 4 | +Problem :Linked List: Add Two Numbers in Python |
| 5 | +Issue Number : #621 |
| 6 | +Problem statement : |
| 7 | +
|
| 8 | +Explanation of the below python code : |
| 9 | +
|
| 10 | +This code implements a solution for the "Add Two Numbers" problem on LeetCode, where the inputs are two non-empty linked lists representing two non-negative integers. The goal is to return the sum of the two integers as a linked list. |
| 11 | +
|
| 12 | +The function takes in two linked lists, l1 and l2, and creates a new linked list represented by the variable 'dummy' which will store the sum. The current node of the new linked list is represented by the variable 'curr'. The variable 'carry' stores the carry-over value from the previous sum. |
| 13 | +
|
| 14 | +The function then loops through both input linked lists, l1 and l2, as well as any carry-over value from the previous sum. It adds the values of the corresponding nodes in l1 and l2 (if they exist) and any carry-over value from the previous sum. It then calculates the carry-over value and the new value for the current node by dividing the sum by 10 and taking the remainder. The carry-over value is then updated for the next iteration of the loop. |
| 15 | +
|
| 16 | +The new node with the calculated value is added to the end of the new linked list using the 'curr' variable. 'curr' is then updated to point to the new node. Once the loop is completed, the new linked list is returned, but without the initial 'dummy' node, as the first node in the list is actually the second node in the sequence. |
| 17 | +
|
| 18 | +Overall, the function creates a new linked list representing the sum of the input linked lists, using a carry-over value to handle any values greater than 10. It runs in linear time in the length of the input linked lists, making it an efficient solution. |
| 19 | +
|
| 20 | +
|
| 21 | +''' |
| 22 | + |
| 23 | +-------------------------------------------------------------------------//python code begins here---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# Definition for singly-linked list. |
| 26 | +# class ListNode: |
| 27 | +# def __init__(self, val=0, next=None): |
| 28 | +# self.val = val |
| 29 | +# self.next = next |
| 30 | +class Solution: |
| 31 | + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: |
| 32 | + dummy = ListNode() |
| 33 | + curr = dummy |
| 34 | + carry = 0 |
| 35 | + while l1 or l2 or carry: |
| 36 | + val = carry |
| 37 | + if l1: |
| 38 | + val += l1.val |
| 39 | + l1 = l1.next |
| 40 | + if l2: |
| 41 | + val += l2.val |
| 42 | + l2 = l2.next |
| 43 | + carry, val = divmod(val, 10) |
| 44 | + curr.next = ListNode(val) |
| 45 | + curr = curr.next |
| 46 | + return dummy.next |
0 commit comments