前言
本人正处于大三,是一名安全方向的学生。正准备考研,为了能够缓解考研的压力,所以利用复习之余刷刷leetcode算法,重新拾起学习安全以来落下的算法。无耐复习紧张,只能每周更新一篇,从简单开始,如有错误,还请谅解提醒!希望自己的考研过程中不在煎熬,希望大家能够有所收获!
题目
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 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
题目数据保证列表表示的数字不含前导零
链接:https://leetcode-cn.com/problems/add-two-numbers
我的思路
其实这里思想主要就是平时我们运算的思路,先算个位,再算十位…
题目给处理数据结构,同时也给出了相应的数据,还比较简单,直接相加即可。
我的代码
C
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode* result;
struct ListNode* head;
result = NULL;
head = NULL;
int d = 0;
while(l1||l2){
int a = l1?l1->val:0;
int b = l2?l2->val:0;
struct ListNode* s = malloc(sizeof(struct ListNode));
s->val = (a+b+d)%10;
d = (a+b+d)/10;
s->next = NULL;
if(result==NULL){
result = head = s;
}
else{
result->next = s;
result = s;
}
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
if(d>0){
struct ListNode* s = malloc(sizeof(struct ListNode));
s->val = d;
s->next = NULL;
result->next = s;
}
return head;
}
python3
# 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: ListNode, l2: ListNode) -> ListNode:
head = ListNode(l1.val + l2.val)
cur = head
while l1.next or l2.next:
l1 = l1.next if l1.next else ListNode()
l2 = l2.next if l2.next else ListNode()
cur.next = ListNode(l1.val + l2.val + cur.val // 10)
cur.val = cur.val % 10
cur = cur.next
if cur.val >= 10:
cur.next = ListNode(cur.val // 10)
cur.val = cur.val % 10
return head
leetcode官方题解
C
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL, *tail = NULL;
int carry = 0;
while (l1 || l2) {
int n1 = l1 ? l1->val : 0;
int n2 = l2 ? l2->val : 0;
int sum = n1 + n2 + carry;
if (!head) {
head = tail = malloc(sizeof(struct ListNode));
tail->val = sum % 10;
tail->next = NULL;
} else {
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = sum % 10;
tail = tail->next;
tail->next = NULL;
}
carry = sum / 10;
if (l1) {
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if (carry > 0) {
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = carry;
tail->next->next = NULL;
}
return head;
}
C++
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = nullptr, *tail = nullptr;
int carry = 0;
while (l1 || l2) {
int n1 = l1 ? l1->val: 0;
int n2 = l2 ? l2->val: 0;
int sum = n1 + n2 + carry;
if (!head) {
head = tail = new ListNode(sum % 10);
} else {
tail->next = new ListNode(sum % 10);
tail = tail->next;
}
carry = sum / 10;
if (l1) {
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if (carry > 0) {
tail->next = new ListNode(carry);
}
return head;
}
};
Java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, tail = null;
int carry = 0;
while (l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
head = tail = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if (carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
}
总结
小编java学的不够深,所以只能给出官方题解了,后续还有给出其他语言结果,重要的不是用什么语言写,重要的是掌握算法思想。小编时间有限,个人代码和与运行速度都比不上官方代码,同时不能给出每种语言的代码,请谅解,希望你能有所收获!
建议有时间的小伙伴在自己电脑上实现,不仅仅实现这一个算法,需要实现其数据结构,这样你会有更多的收获。
868



被折叠的 条评论
为什么被折叠?



