Problem
Problem page: https://leetcode.com/problems/add-two-numbers/
Topics: Linked List, Math
Difficulty: Medium
Similar problems: (M) Multiply Strings, (E) Add Binary
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Analysis
I overthought it at first. I thought the head of the list held the most significant digit, so I was going to use recursion. Then I realized the head held the least significant digit, which brought the difficulty down to almost 0. Watch the carry, especially if there's one at the end.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-add-two-numbers/
