2. 两数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
Python3版
### 解题思路
1.链表要记得设置指针
2.若有一个链表的指针已经指到末尾,则一直输出0,直至两个链表都指到末尾
### 代码
```python3
# 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:
l3 = ListNode(0)
t3 = l3
t1, t2 = l1, l2
c = 0
while t1 or t2:
x = t1.val if t1 else 0
y = t2.val if t2 else 0
s = x + y + c
t3.next = ListNode(s%10)
t3 = t3.next
c = 1 if s > 9 else 0
if t1:
t1 = t1.next
if t2:
t2 = t2.next
if c:
t3.next = ListNode(1)
return l3.next
C语言版
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode *l3 = (struct ListNode*)malloc(sizeof(struct ListNode));
l3->next = NULL;//初始一定要设成NULL,否则出错
struct ListNode *head = l3;//初始将head = l3
int i=0,ret = 3;//ret是用来形容各种状态的一个值,初始代表l1与l2均不为NULL
while( ret != 0 ){
struct ListNode *p;
if( i != 0 ) {//因为这个链表是没有头结点的,所以i==0时p是不需要malloc的
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->next = NULL;
}
int sum;
static int sum1;//用来表示下一位需要进多少值
if( ret == 3 ) sum = (l1->val + l2->val) % 10;
if( ret == 1 ) sum = (l1->val) % 10;//此时l2为NULL
if( ret == 2 ) sum = (l2->val) % 10;//此时l1为NULL
if( ret == 4 ) sum = 0;//此时l1与l2均为NULL,但是sum1不为0
if( i == 0 ){
l3->val = sum;//第一次直接给l3的val赋值
}
else{
p->val = (sum+sum1) % 10;
sum1 = (sum+sum1) / 10;/*这里是很重要的一步,sum1在这里顺便被初始化。防止例如sum=9,sum1=1的情况出现。*/
head->next = p;
head = p;
}
if( ret == 3 )sum1 += (l1->val + l2->val) / 10;
if( ret == 2 )sum1 += (l2->val) / 10;
if( ret == 1 )sum1 += (l1->val) / 10;
if( ret == 4 )sum1 = 0;
if( ret != 2 && ret != 4 ) l1 = l1->next;
if( ret != 1 && ret != 4 ) l2 = l2->next;
if( l1 || l2 ){
if( l1 == NULL ){
ret = 2;
}
if( l2 == NULL ){
ret = 1;
}
}
else{
if( sum1 ) ret = 4;//这里是为了l1与l2皆为NULL但是sum1有值的情况设定的
else ret = 0;
}
i++;
}
return l3;
}
C++版
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(-1);//头节点
ListNode* curr=head;//游标
int sum=0;
int carry=0;//进位标志
while(l1!=NULL||l2!=NULL)
{
sum=0; //最开始要清零
if(l1!=NULL)
{
sum+=l1->val;
l1=l1->next;
}
if(l2!=NULL)
{
sum+=l2->val;
l2=l2->next;
}
if(carry==1)
{
sum++;
}
carry=sum/10;
ListNode* temp=new ListNode(sum%10);
curr->next=temp;
curr=temp;
}
if(carry==1)//最后要判断进位
{
curr->next=new ListNode(1);
}
return head->next;
}
};
Java版
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
import java.math.BigInteger;
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
String a = "" ;
String b = "" ;
while(l1 !=null){
a = String.valueOf(l1.val) + a;
l1 = l1.next;
}
BigInteger inta = new BigInteger(a);
System.out.println("inta = "+inta.toString());
while(l2 !=null){
b = String.valueOf(l2.val) + b;
l2 = l2.next;
}
BigInteger intb = new BigInteger(b);
System.out.println("intb = "+intb.toString());
BigInteger c = inta.add(intb);
System.out.println("c = "+c.toString());
List<ListNode> list = new ArrayList<>();
String tmpc = String.valueOf(c);
for(int i=tmpc.length()-1;i>=0;i--){
int tmp = Integer.valueOf(String.valueOf(tmpc.charAt(i)));
System.out.println("tmp = "+tmp);
list.add(new ListNode(tmp));
}
for(int j=0;j<list.size()-1;j++){
list.get(j).next = list.get(j+1);
}
return list.get(0);
}
}