2. 两数相加:
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
样例 1:
输入:
l1 = [2,4,3], l2 = [5,6,4]
输出:
[7,0,8]
解释:
342 + 465 = 807.
样例 2:
输入:
l1 = [0], l2 = [0]
输出:
[0]
样例 3:
输入:
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:
[8,9,9,9,0,0,0,1]
提示:
- 每个链表中的节点数在范围
[1, 100]
内 0 <= Node.val <= 9
- 题目数据保证列表表示的数字不含前导零
分析:
- 面对这道算法题目,二当家的陷入了沉思。
- 沉思了一会儿,好像没什么需要沉思的,直接模拟就可以了,重拳出击。
题解:
rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn add_two_numbers(mut l1: Option<Box<ListNode>>, mut l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = ListNode::new(0);
let mut t = &mut dummy;
let mut v = 0;
while l1.is_some() || l2.is_some() || v > 0 {
l1 = l1.and_then(|ln| {
v += ln.val;
ln.next
});
l2 = l2.and_then(|ln| {
v += ln.val;
ln.next
});
t.next = Some(Box::new(ListNode::new(v % 10)));
t = t.next.as_mut().unwrap();
v /= 10;
}
return dummy.next;
}
}
go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := ListNode{Val: 0}
t := &dummy
v := 0
for l1 != nil || l2 != nil || v > 0 {
if l1 != nil {
v += l1.Val
l1 = l1.Next
}
if l2 != nil {
v += l2.Val
l2 = l2.Next
}
t.Next = &ListNode{Val: v % 10}
t = t.Next
v /= 10
}
return dummy.Next
}
c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy = ListNode(0);
ListNode *t = &dummy;
int v = 0;
while (l1 || l2 || v) {
if (l1) {
v += l1->val;
l1 = l1->next;
}
if (l2) {
v += l2->val;
l2 = l2->next;
}
t->next = new ListNode(v % 10);
t = t->next;
v /= 10;
}
return dummy.next;
}
};
c
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode *dummy = malloc(sizeof(struct ListNode));
struct ListNode *t = dummy;
int v = 0;
while (l1 || l2 || v) {
if (l1) {
v += l1->val;
l1 = l1->next;
}
if (l2) {
v += l2->val;
l2 = l2->next;
}
t->next = malloc(sizeof(struct ListNode));
t->next->val = v % 10;
t = t->next;
v /= 10;
}
t->next = NULL;// 这里很重要,因为malloc申请的内存不进行初始化
t = dummy;
dummy = dummy->next;
free(t);// 前面申请了个哑结点,这里还要释放,烦哦
return dummy;
}
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = t = ListNode(0)
v = 0
while l1 or l2 or v:
if l1:
v += l1.val
l1 = l1.next
if l2:
v += l2.val
l2 = l2.next
t.next = ListNode(v % 10)
t = t.next
v //= 10
return dummy.next
java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode t = dummy;
int v = 0;
while (l1 != null || l2 != null || v > 0) {
if (l1 != null) {
v += l1.val;
l1 = l1.next;
}
if (l2 != null) {
v += l2.val;
l2 = l2.next;
}
t.next = new ListNode(v % 10);
t = t.next;
v /= 10;
}
return dummy.next;
}
}
非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://developer.aliyun.com/profile/sqd6avc7qgj7y 博客原创~