Add Two Numbers
To solve this coding challenge, we need to create a function that will take two linked lists, each representing a non-negative integer (with digits stored in reverse order), and return a new linked list representing their sum.
Define the function
which takes two linked lists
and
:
The
class helps in building the nodes for the linked lists.
The
function performs a parallel traversal of both input linked lists while managing the carry resulting from the addition. Each resulting digit is appended to a new linked list.
The use of a dummy node allows easy manipulation and return of the resulting linked list without special-case handling of the head node.
By following these steps, you can solve the challenge of adding two numbers represented by linked lists, ensuring both correctness and simplicity in managing the sum and carry of each digit.
Explanation
We'll implement the solution with the following approach:-
Define the ListNode class
: This class will represent each node in the linked list. Each node has a value (
val
next
-
Initialize a dummy node
: This dummy node will help us easily return the resulting linked list later. We will also have a current pointer (
curr
carry
- Traverse both linked lists simultaneously : We'll use a while-loop to traverse both linked lists and continue until we've processed all nodes of both lists and handled any remaining carry.
- Sum the corresponding nodes : For each node in both linked lists, add their values along with any carry from the previous addition. If any of the linked lists is shorter, treat the missing values as 0.
-
Calculate new digit and carry
: Use the divmod function to get the new digit value (
val
- Move to the next nodes : Move to the next nodes in both input linked lists (if available) and continue the process until all nodes are processed.
- Return the result : Finally, return the linked list starting from the node next to the dummy node.
-
Define a class
ListNode
class ListNode:
# Constructor to initialize the node with a value and a next pointer
def __init__(self, val=0, next=None):
self.val = val # Value of the node
self.next = next # Pointer to the next node
addTwoNumbers
l1
l2
def addTwoNumbers(l1, l2):
# Initialize the dummy node and the current pointer
dummy = ListNode()
curr = dummy
carry = 0
# Loop until both linked lists are fully traversed and there is no carry left
while l1 is not null or l2 is not null or carry is not 0:
# Start with the carry value
total_sum = carry
# Add the values from the nodes of l1 and l2 if they exist
if l1 is not null:
total_sum = total_sum + l1.val
l1 = l1.next # Move to the next node in l1
if l2 is not null:
total_sum = total_sum + l2.val
l2 = l2.next # Move to the next node in l2
# Calculate the new digit and the new carry
carry, digit_value = divmod(total_sum, 10)
# Create a new node with the digit value and attach to the result linked list
curr.next = ListNode(val=digit_value)
curr = curr.next # Move the current pointer to the new node
# Return the result linked list starting from the node after the dummy
return dummy.next
Explanation:
ListNode
addTwoNumbers