【LeetCode OJ】Add Two Numbers
题目: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;
}
}
【LeetCode OJ】Add Two Numbers的更多相关文章
- 【LeetCode练习题】Add Two Numbers
链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
随机推荐
- python __getattr__
1.__getattr__ 方法的作用:当调用不存在的属性,就会调用__getattr__()方法: 当一般位置找不到attribute的时候,会调用getattr,返回一个值或AttributeEr ...
- 技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)
一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这 ...
- 【Python】CentOs7 Python3安装Openssl以及解决ssl问题
一.安装OpenssL 1.下载的压缩包放在根目录 wget http://www.openssl.org/source/openssl-1.0.2j.tar.gz 2.在文件夹下解压缩,得到open ...
- linux下locale中的各环境变量的含义
本文来自:http://blog.sina.com.cn/s/blog_406127500101dk26.html Locale是软件在运行时的语言环境, 它包括语言(Language), 地域 (T ...
- The required Server component failed to start so Tomcat is unable to start问题解决
问题出现: Server Tomcat v8.5 Server at localhost failed to start. 或者The required Server component faile ...
- CentOS 6.9编译安装Erlang
转自http://www.jb51.net/article/59823.htm 这篇文章主要介绍了CentOS 6.5源码安装Erlang教程,本文讲解了源码编译安装的过程和遇到的一些错误处理方法,需 ...
- Android开发之获取相册照片和获取拍照照片二
转至 http://blog.csdn.net/beyond0525/article/details/8940840 上一篇文章中讲解了照相机获取照片的时候遇到了可能取得的uri为null的状态,并给 ...
- ASM - ByteCode插件下载、安装以及相关遇到的问题
http://asm.ow2.org/eclipse/index.html http://download.forge.objectweb.org/eclipse-update/ http://for ...
- Linux下的rename命令
Dos/Windows下,对文件改名用rename.而书上说,Linux下对文件或目录改名该用mv.我一直也是这样做的,却忽略了Linux下也有个叫rename的命令.都是rename,但功能上就有点 ...
- [mysql] mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
From: http://www.ttlsa.com/php/deprecated-mysql-connect/ php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5 ...