http://oj.leetcode.com/problems/add-two-numbers/

将用链表表示的两个数相加,(2 -> 4 -> 3) + (5 -> 6 -> 4) 就是342 + 465.刚开始把题目给理解错了,做复杂了。

主要是对指针的理解。

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
int sum;
int carry = ; ListNode *ans = l1;
ListNode *ans3 = ans;
//最后结果存到l1 ans
while(l1&&l2)
{
sum = l1->val + l2->val + carry;
carry = sum/;
if(carry)
l1->val = sum - ;
else
l1->val = sum;
l1 = l1->next;
l2 = l2->next;
}
if(l1==NULL &&l2==NULL)
{
if(carry == )
return ans; //找到最后一个节点
while(ans3->next)
ans3 = ans3->next;
ListNode *temp = new ListNode();
ans3->next = temp;
return ans;
}
//将两种情况化成一种
if(l1 == NULL)
{
while(ans3->next)
ans3 = ans3->next;
ans3->next = l2;
l1 = l2;
l2 = NULL;
} //要看最高位进位的情况
if(carry == )
return ans; while(carry == && l1)
{ l1->val += carry;
if(l1->val > )
{
l1->val = l1->val %;
carry = ;
l1 = l1->next;
}
else
return ans; }
if(carry == )
{
while(ans3->next)
ans3 = ans3->next;
ListNode *temp = new ListNode();
ans3->next = temp;
} return ans;
}
int main()
{
ListNode *l1 = new ListNode();
ListNode *n1 = new ListNode();
ListNode *n5 = new ListNode(); ListNode *l2 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n6 = new ListNode();
//ListNode *n8 = new ListNode(8);
l1->next = n1;
n1->next = n5;
l2->next = n4;
n4->next = n6;
//n6->next = n8;
Solution myS;
ListNode *temp = myS.addTwoNumbers(l2,l1);
return ;
}

囧,读复杂了,还做了个链表的翻转。

ListNode *reverse(ListNode * l2)
{
ListNode *n1,*n2,*before;
n1 = l2;
n2 = n1->next;
before = NULL;
while(n2)
{
n1->next = before;
before = n1;
n1 = n2;
n2 = n2->next;
}
n1->next = before;
return n1;
}

三个指针做链表的反转。

LeetCode OJ--Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

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

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

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

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

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

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

随机推荐

  1. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

  2. C++简易酒店管理系统,实现(查询、入住、退房、楼层选择、退出)功能

    #include <iostream> #include <string.h> #include <stdlib.h> void enter(); void che ...

  3. ubuntu中卸载没有安装完全的软件包

    sudo apt-get autoclean sudo apt-get clean sudo apt-get autoremove

  4. Python基础——判断和循环

    判断 缩进代替大括号. 冒号(:)后换号缩进. if test=100 if test>50: print('OK') print('test') if-elif-else test=50 if ...

  5. iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结

    我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...

  6. private virtual in c++

    source from http://blog.csdn.net/steedhorse/article/details/333664 // Test.cpp #include <iostream ...

  7. Aizu 2450 Do use segment tree 树链剖分

    题意: 给出一棵\(n(1 \leq n \leq 200000)\)个节点的树,每个节点有一个权值. 然后有\(2\)种操作: \(1 \, a \, b \, c\):将路径\(a \to b\) ...

  8. 如何通过 Vue-Cli3 - Vuex 完成一个 TodoList

    昨天大概粗糙的了解了一下Vue的概况之后,并没有从框架.语法的细节来进一步学习.那今天通过一个简单的实例来继续完善一下Vue这方面的空白,用一些看得见的效果摸的着的代码在不断完成小目标的过程中慢慢消化 ...

  9. python - 接口自动化测试 - contants - 常量封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: contants.py @ide: PyCharm Com ...

  10. selenium - 常用等待操作

    # 4. 等待操作 # 强制等待from time import sleepsleep(10) # 隐性等待# 设置最长等待时间,在这个时间在只要有个时间点加载完成,则执行下一步代码,比sleep智能 ...