(LinkedList)2. 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null) //这题看起来简单,但是花了很长时间啊,链表要遍历几次,但是这样写代码最短?实在不想再写了
return l1;
int temp = 0;
ListNode p = l1;
ListNode q = l2;
ListNode longList = l1;
ListNode la = l1;
while (p != null && q != null) {
p = p.next;
q = q.next;
}
if (q == null) {
q = l1;
p = l2;
longList = l1;
} else {
q = l2;
p = l1;
longList = l2;
}
while (p != null && q != null) {
if (p.val + q.val + temp >= 10) {
q.val = p.val + q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + p.val + temp;
temp = 0;
}
p = p.next;
q = q.next;
}
while (q != null) {
if (q.val + temp >= 10) {
q.val = q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + temp;
temp = 0;
}
q = q.next;
}
if (temp == 1) {
ListNode last = new ListNode(1);
last.next = null;
p = longList;
while (p.next!= null)
p = p.next;
la = p;
la.next = last;
} return longList;
}
}
(LinkedList)2. Add Two Numbers的更多相关文章
- Add Two Numbers I & II
Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
随机推荐
- 网页中插入QQ在线功能
网页中插入QQ在线功能 本随笔记录的是网页中如何插入qq在线聊天,这里讲解的是 普通QQ在线聊天操作. 例:第一种方式 使用 tencent://message/?uin=QQ号码&Site ...
- imx6q uboot启动流程牛人的图片(转)
- AT&T asm之Qt使用
#include <stdio.h> #include <string.h> /* 函数名: att_asm_p(); 功能:类似于Intel汇编功能:mov ebx, [OS ...
- C#面向对象学习笔记概要
1.面向对象不是取代面向过程的. 2.面向对象的三个特性:封装.继承.多态. 3.字段.方法.属性(后面讲)都可以叫做类的成员,他们都需要定义访问级别.访问级别的用处在于控制成员在哪些地方可以被访问, ...
- JAVA ,Map接口 ,迭代器Iterator
1. Map 接口概述 java.util.Map 接口描述了映射结构, Map 接口允许以键集.值集合或键 - 值映射关系集的形式查看某个映射的内容. Java 自带了各种 Map 类. 这些 ...
- [深入Python]sys.modules
Python中所有加载到内存的模块都放在sys.modules.当import一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将模块的名字加入到正在调用import的模块的Loc ...
- UE4 异步资源加载
http://blog.csdn.net/pizi0475/article/details/48178861 http://blog.sina.com.cn/s/blog_710ea1400102vl ...
- HDU5128 细心、细心、细心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给你n(n < 30)个点的坐标,然后让你求出这n个点能构成的两个最大矩形的面积,有 ...
- Python培训12期-day2作业-购物车
#!/usr/bin/env python import sys import os import getpass 商品={ '图书': { "<Puppet实战>": ...
- git error
一,今天在上传代码时出错: $ git push -u origin mastererror: The requested URL returned error: 403 Forbidden whil ...