【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 ...
随机推荐
- Java如何使用线程解决死锁?
在Java编程中,如何使用线程解决死锁? 以下示例演示如何使用线程的概念解决死锁问题. // from W w w .Y I I b AI.c o M package com.yiibai; impo ...
- Java如何以短格式显示月份?
在Java中,如何显示短格式的月份名称? 使用DateFormatSymbols().DateFormatSymbols类的getShortMonths()方法,本示例显示了几个月的简写名称. pac ...
- e834. 设置JTabbedPane中卡片的位置
The tabs of a tabbed pane can be placed on one of the four edges of its container. By default, when ...
- Java是一门面向对象编程语言的理解
Java是一门面向对象编程语言. 不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. Java语言作为静态面向对象编程语言的 ...
- 一个类似于postman的协议测试工具
协议测试工具使用postman相当便捷,不过有一个问题,就是每个人都要装一个这个东西,并且测试文件导来导去,还是觉得麻烦了点. 最重要的是postman不能修改,有一些定制功能postman明显力不从 ...
- CI框架 -- 核心文件 之 Output.php(输出类文件)
CI输出类Output.php的功能是将最终web页面发送给浏览器,这里面的东西可能是你用的最少的.你使用装载器加载了一个视图文件, 这个视图文件的内容会自动传递给输出类对象, 然后呢,在方法执行完毕 ...
- Python property,属性
參考资料 http://www.ibm.com/developerworks/library/os-pythondescriptors/ 顾名思义,property用于生成一个属性.通过操作这个属性. ...
- C#.NET MVC 枚举转dictionary自动装载生成下拉框
/// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...
- MATLAB:读取mat文件中物体的三维坐标,显示三维模型
在MATLAB中建立一个脚本show3Dmat.m文件,编写代码: clc; clear; %%read 3D data load('E:\博士\深度学习与三维重建\代码实现\3DRecGAN\X_Y ...
- Mybatis最入门---数据库的下载与安装
[一步是咫尺,一步即天涯] 近期.因为工作进度调整,之前的Spring教程就先临时告一段落了,兴许找个时间继续更新,假设有那位看官想了解某个内容的,敬请留言,大家一起学习. 作为数据库工具的使用开篇. ...