题目链接:https://leetcode-cn.com/problems/add-two-numbers/
题目描述:
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
思路:
模拟过程
关键要处理进位的问题
一位数和一位数相加,大于10时候,除以10,商为进位数,余数为该位的数.
时间复杂度:\(O(n)\)
代码:
python版
# 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:
dummy = ListNode(0)
p = dummy
carry_digit = 0
p1 = l1
p2 = l2
while p1 and p2:
tmp = p1.val + p2.val + carry_digit
quotient = tmp // 10
remainder = tmp % 10
p.next = ListNode(remainder)
carry_digit = quotient
p1 = p1.next
p2 = p2.next
p = p.next
while p1:
tmp = p1.val + carry_digit
quotient = tmp // 10
remainder = tmp % 10
p.next = ListNode(remainder)
carry_digit = quotient
p1 = p1.next
p = p.next
while p2:
tmp = p2.val + carry_digit
quotient = tmp // 10
remainder = tmp % 10
p.next = ListNode(remainder)
carry_digit = quotient
p2 = p2.next
p = p.next
if carry_digit:
p.next = ListNode(carry_digit)
return dummy.next
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode *p = &dummy;
int carry_digit = 0;
while(l1 || l2 || carry_digit){
int tmp = (l1 ? l1->val:0) + (l2 ? l2->val:0) + carry_digit;
carry_digit = tmp / 10;
int remainder = tmp % 10;
p->next = new ListNode(remainder);
p = p->next;
l1 = l1 ? l1->next:l1;
l2 = l2 ? l2->next:l2;
}
return dummy.next;
}
};
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 dummy = new ListNode(0);
ListNode p = dummy;
int carry_digit = 0;
while (l1 != null || l2 != null || carry_digit != 0){
int tmp = (l1!=null?l1.val:0)+(l2!=null?l2.val:0)+carry_digit;
carry_digit = tmp / 10;
p.next = new ListNode(tmp%10);
p = p.next;
l1 = l1 != null ? l1.next:l1;
l2 = l2 != null ? l2.next:l2;
}
return dummy.next;
}
}
大家可以关注我的知乎专栏,你的关注是我变强的动力.
并向我提出宝贵的建议