题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中。

思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会不会还剩进位。

c++的链表,题目已经给了一个挺好的例子:

struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};

对于创建链表可以使用一个头节点来一直指向这个链表,头节点中没有数据

ListNode *l1,*l1head;
l1=new ListNode();
l1head=l1; for(int i=;i<n;i++)
{
scanf("%d",&temp);
ListNode *tempnode=new ListNode(temp);
l1->next=tempnode;
l1=l1->next;
}

接下来直接进行比较,情况考虑周全即可,比较危险的数据有

[5],[5] ; [1],[99]

 class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *res, *head;
res = new ListNode();
head = res;
int flag = ;
while (l1 != NULL && l2 != NULL) {
ListNode *tempNode = new ListNode();
int temp = l1->val + l2->val + flag;
if (temp >= ) {
flag = ;
tempNode->val = temp % ;
} else {
flag = ;
tempNode->val = temp;
} l1 = l1->next;
l2 = l2->next;
res->next = tempNode;
res = res->next;
}
while (l1 != NULL) {
ListNode *tempNode = new ListNode(); if (flag == ) { tempNode->val = l1->val + flag;
if (tempNode->val >= ) {
flag = ;
tempNode->val = tempNode->val % ;
} else {
flag = ;
}
l1 = l1->next;
res->next = tempNode;
res = res->next;
} else {
res->next = l1;
break;
} }
while (l2 != NULL) {
ListNode *tempNode = new ListNode(); if (flag == ) { tempNode->val = l2->val + flag;
if (tempNode->val >= ) {
flag = ;
tempNode->val = tempNode->val % ;
} else {
flag = ;
}
l2 = l2->next;
res->next = tempNode;
res = res->next;
} else {
res->next = l2;
break;
} }
if (flag == ) {
ListNode *tempNode = new ListNode();
res->next = tempNode;
}
return head->next;
}
};

Add Two Numbers

PS :

调用和返回都使用了p->next的方式,因为没有把头指针传进去。

Add Two Numbers - C++链表操作的更多相关文章

  1. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

  2. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  3. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  4. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  5. LeetCode OJ:Add Two Numbers (相加链表之数)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. 445. Add Two Numbers II 链表中的数字求和

    [抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...

  7. 链表求和12 · Add Two Numbers

    反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

随机推荐

  1. Win32中安全的子类化(翻译)

    关于子类化的话题虽然有些旧,但它至今仍然不失为一种开发Windows的强有力技术,在MFC的内核.甚至.NET的内核中都离不开它,希望本连载能对Windows开发的爱好者有所帮助. 原文标题:Safe ...

  2. vim-配置文件

    " " Last Change: 2010年08月02日 15时13分 " " Version: 1.80 " "============= ...

  3. SICP 习题 (1.10)解题总结

    SICP 习题 1.10 讲的是一个叫“Akermann函数”的东西,去百度查可以查到对应的中文翻译,叫“阿克曼函数”. 就像前面的解题总结中提到的,我是一个数学恐惧者,看着稍微复杂一点的什么函数我就 ...

  4. JavaScript之arguements对象学习

    简介:在JavaScript中,有一个特殊的对象-Arguements对象,它是当前函数的一个内置属性,它类似与Array对象(数组形式),但不是Array的一个实例.下面通过代码来论证: <s ...

  5. C#经典之Application.DoEvents()的使用

    最近做了一个文件上传的模块,因为牵扯到电脑文件的扫描,想做一个实时显示当前扫面文件的功能,就类似于360文件扫描时的效果,本来打算用多线程来实现,但是方法太多没有实现,后来在程序中进行控制,由于文件太 ...

  6. [Puzzle] 蚂蚁路线碰撞问题

    有这么一道题目, 看下面的图, 假设有一条直线, 每个叉叉上有一只蚂蚁, 它们会随机选择一个方向, 向前或者向后移动, 每次走一格, 前进中当两只蚂蚁相遇, 它们会掉头, 问: 全部蚂蚁都走出去的最长 ...

  7. trangle

    #include<iostream> #include<algorithm> using namespace std; int main() { int a,b,c; whil ...

  8. perl正则表达式第三周笔记

    正则引擎的分类 正则引擎的分类 正则引擎的分类主要分两种: DFA:egrep.awk.lex.flex NFA:.NET.PHP.Perl.Ruby.Python.GNU Emacs.ed.sec. ...

  9. iScroll 4,把禁掉的:active样式还给我~

    iScroll这个移动端的滚动神器大家都非常熟悉了,直到现在仍是实现移动端Web滚动的首选方案之一... 当我接触移动端Web时iScroll已经有两个版本了,iScroll 4和iScroll 5, ...

  10. java.el.PropertyNotFoundException解决方法

    今天在开发中遇到了java.el.PropertyNotFoundException异常,检查JSP页面.Action.Bean.都没有发现错误 在网上搜了一下可能是我的bean不是一个标准的bean ...