Knowee
Questions
Features
Study Tools

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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 itsel

Question

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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 itsel

🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can follow the steps below:

  1. Initialize a dummy node and a current node to keep track of the sum.
  2. Initialize carry as 0 to keep track of any carry-over from addition.
  3. Traverse both linked lists simultaneously until both lists are exhausted.
  4. At each step, add the values of the current nodes from both lists, along with the carry.
  5. Calculate the sum and update the carry if necessary.
  6. Create a new node with the sum % 10 and set it as the next node of the current node.
  7. Move the current node and both input lists to their next nodes.
  8. After the traversal, if there is still a carry, create a new node with the carry and set it as the next node of the current node.
  9. Return the next node of the dummy node as the head of the resulting linked list.

Here is the step-by-step solution in code:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode(0)
    current = dummy
    carry = 0
    
    while l1 or l2:
        sum = carry
        
        if l1:
            sum += l1.val
            l1 = l1.next
        if l2:
            sum += l2.val
            l2 = l2.next
        
        carry = sum // 10
        current.next = ListNode(sum % 10)
        current = current.next
    
    if carry:
        current.next = ListNode(carry)
    
    return dummy.next

This solution has a time complexity of O(max(m, n)), where m and n are the lengths of the input linked lists.

This problem has been solved

Similar Questions

ou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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.

2. Add Two NumbersMedium29K5.6KCompaniesYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807.

Given the head pointers of two linked lists, add them and return the new linked list. Each linked list represents an integer number (each node is a digit).

class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dummyHead = ListNode(0) tail = dummyHead carry = 0 while l1 is not None or l2 is not None or carry != 0: digit1 = l1.val if l1 is not None else 0 digit2 = l2.val if l2 is not None else 0 sum = digit1 + digit2 + carry digit = sum % 10 carry = sum // 10 newNode = ListNode(digit) tail.next = newNode tail = tail.next l1 = l1.next if l1 is not None else None l2 = l2.next if l2 is not None else None result = dummyHead.next dummyHead.next = None return result

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list.  You may return any such answer.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.