【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 ...
随机推荐
- C艹复合类型(字符串)
在C艹中有两种字符串形式, 一种是C-风格, 另一种是C艹风格的 初始化: char str[10] = {'a', 'c', 'd', '\0'};char str[20]= “aaa”; stri ...
- Java如何使用catch来处理异常?
在Java编程中,如何使用catch块来处理异常? 此示例显示如何使用catch来处理异常. package com.yiibai; public class UseOfCatch { public ...
- JDBC简单示例代码
本文章教程中将演示如何创建一个简单的JDBC应用程序的示例. 这将显示如何打开数据库连接,执行SQL查询并显示结果. 这个示例代码中涉及所有步骤,一些步骤将在本教程的后续章节中进行说明. 创建JDBC ...
- Windows下安装Oracle12C(一)
1,在官网上下载oracle的压缩文件,两个都要下载. 并两个同时选中解压在一个文件夹里面. 2,解压之后,如下图,点击setup.exe稍等一会儿 ,3,开始安装: 不选点击下一步,或者直接点击下一 ...
- iptables filter表小案例
案例1:把80端口,22端口,21端口放行 22端口指定IP访问,其它IP拒绝. shell脚本实现: [root@centos7 ~]# vim /usr/local/sbin/iptables.s ...
- 编写高质量代码_改善C++程序的150个建议 读书笔记
这几天看了下这本书<编写高质量代码_改善C++程序的150个建议>,觉的蛮有收获的,再次记录下自己以前不清晰的知识点,以供学习. 编写符合标准的main函数 C语言标准规定了main函数的 ...
- 为npm设置代理
npm全称为Node Packaged Modules.它是一个用于管理基于node.js编写的package的命令行工具.其本身就是基于node.js写的,这有点像gem与ruby的关系. 在我们的 ...
- js堆栈与队列简单记忆
在面向对象的程序设计里,一般都提供了实现队列(queue)和堆栈(stack)的方法,而对于JS来说,我们可以实现数组的相关操作,来实现队列和堆栈的功能,看下面的相关介绍. 一 看一下它们的性质,这种 ...
- Lua点号和冒号区别
定义的时候冒号默认接收self参数调用的时候冒号默认传递调用者自己为参数而句号要显示传递或接收self参数 -- 例如:句号定义,需要显示传递或接收 a = { x = } function a.fu ...
- wingIDE设置支持中文注释
用wingIDE快2年了,实在是受不了不支持中文,每次中文都报错,一个小逗号也要查很久,别说中文注释了,在网上找解决办法,原来这么简单! 下面,把破解放大和支持中文支持的方法备份下来,以便以后查看. ...