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
代码:
vector<int> v1;
vector<int> v2;
int num1;
int num2;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
void insertAtTail(ListNode* &reslist,int val)
{
ListNode* p=reslist;
while (p->next)
p=p->next;
ListNode* newNode=(ListNode*)malloc(sizeof(ListNode));
newNode->val=val;
newNode->next=p->next;
p->next=newNode; }
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(l1==NULL&&l2==NULL) return NULL;
ListNode* reslist=NULL;
int jinwei=;
while(l1!=NULL&&l2!=NULL){
if((l1->val+l2->val+jinwei)<){
if(reslist==NULL){
reslist=(ListNode*)malloc(sizeof(ListNode));
reslist->val=l1->val+l2->val+jinwei;
reslist->next=NULL;
}else{
insertAtTail(reslist,l1->val+l2->val+jinwei);
}
jinwei=;
}else{
if(reslist==NULL){
reslist=(ListNode*)malloc(sizeof(ListNode));
reslist->val=l1->val+l2->val+jinwei-;
reslist->next=NULL;
}else{
insertAtTail(reslist,l1->val+l2->val+jinwei-);
}
jinwei=;
}
l1=l1->next;
l2=l2->next;
}
while(l1!=NULL){
if(l1->val+jinwei<){
insertAtTail(reslist,l1->val+jinwei);
jinwei=;
}else{
insertAtTail(reslist,l1->val+jinwei-);
jinwei=;
}
l1=l1->next;
}
while(l2!=NULL){
if(l2->val+jinwei<){
insertAtTail(reslist,l2->val+jinwei);
jinwei=;
}else{
insertAtTail(reslist,l2->val+jinwei-);
jinwei=;
}
l2=l2->next;
}
if(l1==NULL&&l2==NULL&&jinwei!=){
insertAtTail(reslist,jinwei);
}
return reslist;
}
}; void CreateListHead(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v1[j++];
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v1[j++];
p=p->next;
}
}
void CreateListHead2(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v2[j++];
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v2[j++];
p=p->next;
}
}
int main()
{
freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
cin>>num1;
for (int i=;i<num1;++i)
{
int temp;
cin>>temp;
v1.push_back(temp);
}
cin>>num2;
for (int i=;i<num2;++i)
{
int temp;
cin>>temp;
v2.push_back(temp);
}
ListNode* head1=NULL;
CreateListHead(head1,num1);
ListNode* head2=NULL;
CreateListHead2(head2,num2);
Solution so;
ListNode* res=so.addTwoNumbers(head1,head2);
return ;
}
Add Two Numbers(链表)的更多相关文章
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- [LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...
- Add Two Numbers - C++链表操作
题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中. 思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- 链表求和12 · Add Two Numbers
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- poj3685 Matrix
思路: 二分套二分. 矩阵在每一列上是严格递增的,可以利用这一点进行二分. 实现: #include <cstdio> #include <cmath> #include &l ...
- swift VTables
VTables https://github.com/apple/swift/blob/master/docs/SIL.rst#vtables decl ::= sil-vtable sil-vtab ...
- discuz 插件核心函数hookscript分析.
function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = ...
- CAD交互绘制圆弧(com接口)
在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...
- springboot Tomcat connector configured to listen on port 8081 failed to start.
启动报 Tomcat connector configured to listen on port 8081 failed to start. The port may already be in ...
- vue props传值方法
<template> <div class="hello"> <ul> <li v-for="(item, index) in ...
- oracle亲手安装过程
适用于centos6 radhat6版本 1.检查依赖库: rpm -q binutils compat-libcap1 compat-libstdc++ compat-libstdc++.i686 ...
- Angular网络请求的封装
很多时候,我很喜欢angular的编码风格,特别是angular支持typescript之后,完整的生命周期,完美的钩子函数,都是别的语言所无法替代的.这里我来说说我自己的网络请求封装,某种意义上来说 ...
- MySQL-----多对多
多对多: 示例1: 用户表和相亲记录表 用户表 用户id 用户名 性别 1 George 男 2 Elizabeth 女 3 Bruce 男 4 Catherine ...
- .DS_Store的说明
今天清理电脑时,突然发现好像有文件的地方都会出现一个.DS_Store文件,今天有时间,索性就查了一下,并做总结发表一篇吧,怕有什么影响,并未真正实施,仅仅供自己收藏,仅供大家参考. DS_ ...