Leetcode 2 - Add Two Numbers

Problem Description

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 it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

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

Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res =  new ListNode(-1);
        ListNode cur = res;
        int carry = 0;
        
        while (l1 != null || l2 != null) {
            carry = carry + (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
            cur.next = new ListNode(carry % 10);
            cur = cur.next;
            carry /= 10;
        }
        
        if (carry != 0) {
            cur.next = new ListNode(carry);
        }
        
        return res.next;
    }
}

Python:

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

class Solution:
    
    carry = 0
    
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 is None and l2 is None:
            return None if self.carry == 0 else ListNode(self.carry)
        else:
            self.carry += (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val)
            res = ListNode(self.carry % 10)
            self.carry //= 10
            res.next = self.addTwoNumbers(None if l1 is None else l1.next, None if l2 is None else l2.next)
            return res

Go:

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
    var res *ListNode = new(ListNode) // var res *ListNode = &ListNode{0, nil}
    cur := res
    carry := 0
    
    for l1 != nil || l2 != nil || carry != 0 {
        if l1 != nil {
            carry += l1.Val
            l1 = l1.Next
        }
        
        if l2 != nil {
            carry += l2.Val
            l2 = l2.Next
        }
        
        cur.Next = new(ListNode)
        cur = cur.Next
        cur.Val = carry % 10
        carry /= 10
    }
    
    return res.Next
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • 上通信的課就跟室友一起刷題玩,結果讀題目讀了半天,好歹我也是過了四級的人啊,怎么能這樣,干脆百度題目意思…… 題目...
    做夢枯島醒閱讀 286評論 0 0
  • You are given two non-empty linked lists representing two...
    5539閱讀 159評論 0 0
  • 小閨說“我就是不要一個人去廈門,廈門這個城市我一定要和男朋友一起去”。 我們一群人笑她太執(zhí)念,她卻笑笑不語。 其實...
    霧起閱讀 634評論 0 0
  • 真的開始懷疑,我是不是還有寫作的能力,沒有太忙,手上也沒有太多事情,怪往事不堪回首,想寫這篇年終總結已經是2016...
    那么透徹閱讀 312評論 0 1

友情鏈接更多精彩內容