洞察探索如何利用兼容微信生态的小程序容器,实现跨平台开发,助力金融和车联网行业的数字化转型。
782
2022-09-06
2. Add Two Numbers
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 contain 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 -> 8Explanation: 342 + 465 = 807.
Java:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; ListNode head = new ListNode(0); ListNode p = head; int temp = 0; while(l1 != null || l2 != null || temp != 0) { if(l1 != null) { temp += l1.val; l1 = l1.next; } if(l2 != null) { temp += l2.val; l2 = l2.next; } p.next = new ListNode(temp % 10); p = p.next; temp /= 10; } return head.next; }}
/** * 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) { int sum = 0; ListNode res = new ListNode(-1), tail = res; while(l1 != null || l2 != null || sum >= 10) { sum /= 10; if(l1 != null) { sum += l1.val; l1 = l1.next; } if(l2 != null) { sum += l2.val; l2 = l2.next; } tail.next = new ListNode(sum % 10); tail = tail.next; } return res.next; }}
Python:
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: carry = 0 temp = l1 num = 0 while l2 != None or carry != 0: if l2 != None: num = l2.val l1.val = num + l1.val + carry carry = 0 if l1.val > 9: l1.val -= 10 carry = 1 if l1.next == None: if l2 == None: if carry != 0: l1.next = ListNode(0) else: if l2.next != None or carry != 0: l1.next = ListNode(0) if l2 != None: l2 = l2.next l1 = l1.next num = 0 return temp
# 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: dummyHead = ListNode(0) curr, carry = dummyHead, 0 while l1 or l2: sum = 0 if l1: sum += l1.val l1 = l1.next if l2: sum += l2.val l2 = l2.next sum += carry carry = sum // 10 curr.next = ListNode(sum % 10) curr = curr.next if carry > 0: curr.next = ListNode(1) return dummyHead.next;
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dummy = ListNode() cur = dummy carry = 0 while l1 or l2 or carry: v1 = l1.val if l1 else 0 v2 = l2.val if l2 else 0 # compute digit val = v1 + v2 + carry carry = val // 10 val %= 10 cur.next = ListNode(val) # update pointers cur = cur.next l1 = l1.next if l1 else None l2 = l2.next if l2 else None return dummy.next
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */var addTwoNumbers = function(l1, l2) { var head = new ListNode(0); var current = head; var carry = 0; while (l1 || l2) { l1 = l1 || new ListNode(0); l2 = l2 || new ListNode(0); var sum = l1.val + l2.val + carry; if (sum > 9) { carry = 1; sum = sum - 10; } else { carry = 0; } var node = new ListNode(sum); current.next = node; current = node; l1 = l1.next; l2 = l2.next; } if (carry !== 0) { current.next = new ListNode(carry); } return head.next};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~