0
0
DSA Pythonprogramming~20 mins

Add Two Numbers Represented as Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Adding Two Linked Lists Representing Numbers
What is the output linked list after adding the two numbers represented by these linked lists?

List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null

Note: Each node contains a single digit and digits are stored in reverse order.
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode()
    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
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    return dummy.next

# Construct lists
l1 = ListNode(2, ListNode(4, ListNode(3)))
l2 = ListNode(5, ListNode(6, ListNode(4)))

result = addTwoNumbers(l1, l2)
output = []
while result:
    output.append(result.val)
    result = result.next
print(output)
A[7, 10, 7]
B[8, 0, 7]
C[7, 0, 8]
D[5, 10, 7]
Attempts:
2 left
💡 Hint
Add digits node by node, remember to carry over if sum >= 10.
Predict Output
intermediate
2:00remaining
Result of Adding Unequal Length Linked Lists
What is the output linked list after adding these two numbers?

List 1: 9 -> 9 -> 9 -> 9 -> null
List 2: 1 -> null

Digits are stored in reverse order.
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode()
    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
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    return dummy.next

l1 = ListNode(9, ListNode(9, ListNode(9, ListNode(9))))
l2 = ListNode(1)

result = addTwoNumbers(l1, l2)
output = []
while result:
    output.append(result.val)
    result = result.next
print(output)
A[0, 0, 0, 0, 1]
B[0, 0, 0, 1]
C[1, 0, 0, 0, 0]
D[9, 9, 9, 10]
Attempts:
2 left
💡 Hint
Remember to carry over after the last node if needed.
🔧 Debug
advanced
2:00remaining
Identify the Error in Linked List Addition Code
What error will this code raise when adding two linked lists?

Code snippet:
def addTwoNumbers(l1, l2):
    dummy = ListNode()
    current = dummy
    carry = 0
    while l1 or l2:
        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
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    if carry:
        current.next = ListNode(carry)
    return dummy.next
ASyntaxError: invalid syntax
BAttributeError: 'NoneType' object has no attribute 'val'
CNo error, code runs correctly
DTypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens when l1 or l2 is None.
Predict Output
advanced
2:00remaining
Output When Adding Lists with Zero Nodes
What is the output linked list after adding these two numbers?

List 1: 0 -> null
List 2: 0 -> null

Digits are stored in reverse order.
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode()
    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
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    return dummy.next

l1 = ListNode(0)
l2 = ListNode(0)

result = addTwoNumbers(l1, l2)
output = []
while result:
    output.append(result.val)
    result = result.next
print(output)
A[0]
B[]
C[1]
D[0, 0]
Attempts:
2 left
💡 Hint
Adding zero plus zero should give zero.
Predict Output
expert
3:00remaining
Output of Adding Large Numbers with Multiple Carries
What is the output linked list after adding these two numbers?

List 1: 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> null
List 2: 9 -> 9 -> 9 -> 9 -> null

Digits are stored in reverse order.
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode()
    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
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    return dummy.next

l1 = ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9)))))))
l2 = ListNode(9, ListNode(9, ListNode(9, ListNode(9))))

result = addTwoNumbers(l1, l2)
output = []
while result:
    output.append(result.val)
    result = result.next
print(output)
A[8, 9, 9, 9, 9, 9, 9, 1]
B[8, 9, 9, 9, 0, 0, 1]
C[0, 9, 9, 9, 0, 0, 0, 1]
D[8, 9, 9, 9, 0, 0, 0, 1]
Attempts:
2 left
💡 Hint
Add digit by digit with carry, carefully track carries beyond the shorter list.