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.
Question
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.
Solution
Sure, here is a Python solution for the problem:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def addTwoNumbers(l1, l2):
dummy_head = ListNode(0)
p, q, current = l1, l2, dummy_head
carry = 0
while p is not None or q is not None:
x = p.val if p is not None else 0
y = q.val if q is not None else 0
sum = carry + x + y
carry = sum // 10
current.next = ListNode(sum % 10)
current = current.next
if p is not None: p = p.next
if q is not None: q = q.next
if carry > 0:
current.next = ListNode(carry)
return dummy_head.next
This solution works by initializing a dummy head of the result list. Then, we maintain a carry variable to keep track of the carry from the sum of two digits. In each step, we add the current digits and carry, create a new node in the result list, and update the carry. The loop continues until we have processed all the digits of both numbers. If there is a remaining carry at the end, we add an additional node. Finally, we return the next of the dummy head, which is the head of the result list.
Similar Questions
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
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.
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.