Algorithm Add two numbers under Python linked list

topic

gives you two non-empty linked lists representing two non-negative integers. They are stored in reverse order per digit , and each node can only store one digit.

Please add two numbers and return a linked list representing the sum in the same form. You can assume that neither number starts with 0 except for the number 0.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.


Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]


Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

hint:

  • The number of nodes in each linked list is in  [1, 100] range
  • 0 <= Node.val <= 9

Guess you like

Origin blog.csdn.net/qq_37865996/article/details/124394292