LeetCode Add Two Numbers
Problem statement
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 itself.
Problem statement taken from: //leetcode.com/problems/add-two-numbers
Example 1:
Example 2:
Input: l1 = [0], l2 = [0] Output: [0]Example 3:
Constraints:
- The number of nodes in each linked list is in the range [1, 100]. - 0 val - Move l1 to point next node l1 = l1->next - if [ l2 != nil ] - Add l2-> val to sum as sum += l2->val - Move l2 to point next node l2 = l2->next - Initialize new ListNode with last digit of sum new ListNode[ sum % 10 ] - Assign current node next to the new ListNode created above - current->next = new ListNode[ sum % 10 ] - Set current to current->next - Set sum = sum / 10. sum / 10 will be 0 for sum < 10 and 1 for sum >= 10 - Check if sum > 9, if so append a new node with digit 1 to current node. - Return result->next since result is still pointing to current's first position.