2.Add Two Numbers
題目連結
難易度:Medium
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.
思路
Create a empty ListNode first, write a recursive function to dive into l1
, l2
ListNode,
Caculate the sum of l1.val
and l2.val
and the carry from last node, only store the number 1~9 part in the
current node, pass carry to next node, re-run the function until l1
and l2
are all empty.
解題
1 | /** |