【LeetCode OJ】Add Two Numbers

时间:2022-03-06 01:02:54

题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

 #include "stdafx.h"
#include <malloc.h>
#include <iostream>
using namespace std;
typedef struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} }*Lnode;
Lnode create() //尾插法建立链表
{
Lnode head,q,r;
int temp;
head = (struct ListNode *)malloc(sizeof(Lnode));
head->next = NULL;
r = head;
cin >> temp;
while (temp!=-)//输入-1,创建链表结束
{
q = (struct ListNode *)malloc(sizeof(Lnode));
q->val = temp;
r->next = q;
r = q;
cin >> temp;
}
r->next = NULL;
return head;
}
void print(Lnode h) //打印链表
{
Lnode p=h->next;
while (p)
{
cout << p->val<<endl;
p = p->next;
}
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
int flag = ;
ListNode * p,*r;
ListNode * h = (struct ListNode *)malloc(sizeof(ListNode *));
h->next = NULL;
r = h;
int num;
if (l1 == NULL) return l1;
if (l2 == NULL) return l2;
while (l1&&l2)
{
num = l1->val + l2->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else //如果两位数相加大于10,则向前进一位
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num-;
r->next = p;
r = p;
flag = ;
}
l1 = l1->next;
l2 = l2->next;
}
while (l1)
{
num = l1->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num - ;
r->next = p;
r = p;
flag = ;
}
l1 = l1->next; } while (l2)
{
num = l2->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num - ;
r->next = p;
r = p;
flag = ;
}
l2 = l2->next; }
if (flag) //最后再判断一次判断是否有进位
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = ;
r->next = p;
r = p;
}
r->next = NULL;
return h->next;
}
int _tmain(int argc, _TCHAR* argv[]) //测试函数
{
Lnode t1,t2,t;
t1 = create();
t2 = create();
t = addTwoNumbers(t1->next,t2->next);
print(t);
return ;
}

  Java版本:

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode head=new ListNode(0);
head.next=null;
int tag=0;//进位标志
if(l1.val+l2.val<10)
{
head.val=l1.val+l2.val;
}
else
{
head.val=l1.val+l2.val-10;
tag=1;
}
ListNode p=head;
ListNode node1=l1.next;
ListNode node2=l2.next;
while(node1!=null||node2!=null)
{
int sum;
int result;
ListNode node=new ListNode(0);
node.next=null;
p.next=node;
p=node;
if(node1!=null&&node2!=null)
{
sum=node1.val+node2.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node1=node1.next;
node2=node2.next;
}
else if(node1==null&&node2!=null)
{
sum=node2.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node2=node2.next;
}
else if(node1!=null&&node2==null)
{
sum=node1.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node1=node1.next;
continue;
}
}
if(node1==null&&node2==null&&tag==1)
{
ListNode node=new ListNode(1);
node.next=null;
p.next=node;
p=node;
}
return head;
}
}