目前使用的语言:C、C++、python、C#、PHP。
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
来源:力扣(LeetCode)
链接:https://leetcode.cn/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){
int carry = 0;
int sum;
if(l1 == NULL)
{
return l2;
}
if(l2 == NULL)
{
return l1;
}
struct ListNode *l = l1;
sum = l1->val + l2->val;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->val = sum;
while(l1->next && l2->next)
{
sum = l1->next->val + l2->next->val + carry;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->next->val = sum;
l1 = l1->next;
l2 = l2->next;
}
if(l2->next)
{
l1->next = l2->next;
}
while(carry == 1)
{
if(l1->next == NULL)
{
l1->next = (struct ListNode *)malloc(sizeof(struct ListNode));
l1->next->next = NULL;
l1->next->val = carry;
break;
}
sum = l1->next->val + carry;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->next->val = sum;
l1 = l1->next;
}
return l;
}
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) {
int carry = 0;
int sum;
if(l1 == NULL)
{
return l2;
}
if(l2 == NULL)
{
return l1;
}
ListNode *l = l1;
sum = l1->val + l2->val;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->val = sum;
while(l1->next && l2->next)
{
sum = l1->next->val + l2->next->val + carry;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->next->val = sum;
l1 = l1->next;
l2 = l2->next;
}
if(l2->next)
{
l1->next = l2->next;
}
while(carry == 1)
{
if(l1->next == NULL)
{
l1->next = new ListNode(carry);
break;
}
sum = l1->next->val + carry;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else carry = 0;
l1->next->val = sum;
l1 = l1->next;
}
return l;
}
};
C#:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null)
{
return l2;
}
if(l2 == null)
{
return l1;
}
ListNode l = new ListNode();
l = l1;
int sum, carry;
sum = l1.val + l2.val;
if(sum >= 10)
{
sum -= 10;
carry = 1;
}
else
{
carry = 0;
}
l1.val = sum;
while(l1.next != null && l2.next != null)
{
sum = l1.next.val + l2.next.val + carry;
if(sum > 9)
{
sum -= 10;
carry = 1;
}
else carry = 0;
l1.next.val = sum;
l1 = l1.next;
l2 = l2.next;
}
if(l2.next != null)
{
l1.next = l2.next;
}
while(carry == 1)
{
if(l1.next == null)
{
l1.next = new ListNode();
l1.next.val = 1;
break;
}
sum = l1.next.val + carry;
if(sum > 9)
{
sum -= 10;
carry = 1;
}
else carry = 0;
l1.next.val = sum;
l1 = l1.next;
}
return l;
}
}
python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 == None:
return l2
if l2 == None:
return l1
l = l1
sum = l1.val + l2.val
if sum > 9:
sum -= 10
carry = 1
else:
carry = 0
l1.val = sum
while l1.next != None and l2.next != None:
sum = l1.next.val + l2.next.val + carry
if sum > 9:
sum -= 10
carry = 1
else :
carry = 0
l1.next.val = sum
l1 = l1.next
l2 = l2.next
if l2.next != None:
l1.next = l2.next
while carry == 1:
if l1.next == None:
l1.next = ListNode()
l1.next.val = 1
break
sum = l1.next.val + carry
if sum > 9:
sum -= 10
carry = 1
else :carry = 0
l1.next.val = sum
l1 = l1.next
return l
PHP:
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function addTwoNumbers($l1, $l2) {
if($l1 == null)
{
return $l2;
}
if($l2 == null)
{
return $l1;
}
$l = new ListNode();
$l = $l1;
$sum =$l1->val + $l2->val;
if($sum > 9)
{
$sum -= 10;
$carry = 1;
}
else
{
$carry = 0;
}
$l1->val = $sum;
while($l1->next != null && $l2->next != null)
{
$sum = $l1->next->val + $l2->next->val + $carry;
if($sum > 9)
{
$sum -= 10;
$carry = 1;
}
else{
$carry = 0;
}
$l1->next->val = $sum;
$l1 = $l1->next;
$l2 = $l2->next;
}
if($l2->next != null)
{
$l1->next = $l2->next;
}
while($carry == 1)
{
if($l1->next == null)
{
$l1->next = new ListNode();
$l1->next->val = 1;
break;
}
$sum = $l1->next->val + $carry;
if($sum > 9)
{
$sum = $sum - 10;
$carry = 1;
}
else{
$carry = 0;
}
$l1->next->val = $sum;
$l1 = $l1->next;
}
return $l;
}
}