Zepruv › Coding problems

Add Two Numbers II

Medium · Linked List, Math, Stack

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1

Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]

Example 2

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]

Example 3

Input: l1 = [0], l2 = [0]
Output: [0]

Constraints

Starter code (python)

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: Optional[ListNode]
        :type l2: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        

Related problems

Previous: String Compression · Next: Find All Numbers Disappeared in an Array · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.