• Author: Chen Da Yu Tou
  • Project address: Ying-leetCode
  • Mumble: Mmmmm, swipe leetcode irregularly and it will be output as JS TS PY

Topic describes

Give two non-empty linked lists to represent two non-negative integers. Their respective bits are stored in reverse order, and each node can store only one digit.

If we add these two numbers together, a new linked list is returned to represent their sum.

You can assume that neither of these numbers will start with 0, except for the number 0.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Cause: 342 + 465 = 807

Their thinking

The only thing you need to notice is that if the number is greater than 10, you need to go one level down and add the number in the first place. Basically a cycle can be solved. Again, if one list is longer than another, that’s all.

JS version

/** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */
const addTwoNumbers = (l1, l2) = > {
    let l3 = null
    let cache = 0
    let tens = 0
    while (l1 || l2) {
        let total = 0
        if (l1) {
            let l1Head = l1.val
            total += l1Head
            l1 = l1.next
        }
        if (l2) {
            let l2Head = l2.val
            total += l2Head
            l2 = l2.next
        }
        total += tens
        if (total >= 10) {
            total -= 10
            tens = 1
        } else {
            tens = 0
        }
        let node = new ListNode(total)
        if (cache) {
            cache.next = node
            cache = node
        } else {
            l3 = node
            cache = l3
        }
    }
    if (tens === 1) {
        cache.next = new ListNode(1)}return l3
}
Copy the code

TS edition

class ListNode {
    val: number
    next: ListNode | any
    constructor(value: number) {
        this.val = value
        this.next = null}}/** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */
const addTwoNumbers = (l1: ListNode, l2: ListNode) = > {
    let l3: null | ListNode = null
    let cache: ListNode | null = null
    let tens: number = 0
    while (l1 || l2) {
        let total: number = 0
        if (l1) {
            let l1Head = l1.val
            total += l1Head
            l1 = l1.next
        }
        if (l2) {
            let l2Head = l2.val
            total += l2Head
            l2 = l2.next
        }
        total += tens
        if (total >= 10) {
            total -= 10
            tens = 1
        } else {
            tens = 0
        }
        let node = new ListNode(total)
        if (cache) {
            cache.next = node
            cache = node
        } else {
            l3 = node
            cache = l3
        }
    }
    if (tens === 1) {
        cache.next = new ListNode(1)}return l3
}
Copy the code

PY version

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """
        l3 = None
        cache = 0
        tens = 0
        while l1 or l2:
            total = 0
            if l1:
                l1Head = l1.val
                total = total + l1Head
                l1 = l1.next
            if l2:
                l1Head = l2.val
                total = total + l1Head
                l2 = l2.next
            total = total + tens
            if total >= 10:
                total = total - 10
                tens = 1
            else:
                tens = 0
            node = ListNode(total)
            if cache:
                cache.next = node
                cache = node
            else:
                l3 = node
                cache = l3
        if tens == 1:
            cache.next = ListNode(1)
        return l3
Copy the code

If you like to discuss technology, or have any comments or suggestions on this article, you are welcome to add yu Tou wechat friends to discuss. Of course, Yu Tou also hopes to talk about life, hobbies and things with you. You can also scan your wechat account to subscribe to more exciting content.