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) {
ListNode c1 = l1;//定义两个指针变量 c1 c2
ListNode c2 = l2;
ListNode head = new ListNode(0);//定义一个新链表的头结点
ListNode d = head;//新链表的指针变量
int sum = 0;
while(c1 != null || c2 != null) {
if(c1 != null) {
sum = sum + c1.val;
c1 = c1.next;
}
if(c2 != null) {
sum = sum + c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum%10);
d = d.next;
sum = sum /10;
}
if(sum == 1) {
d.next = new ListNode(1);
}
return head.next; }
}
2.Add Two Numbers-两个单链表相加的更多相关文章
- 面试题:Add Two Numbers(模拟单链表)
题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [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 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- python经典面试算法题1.3:如何计算两个单链表所代表的数之和
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...
- [LeetCode] 2. 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 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- leetcode 题解 Add Two Numbers(两个单链表求和)
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【LeetCode每天一题】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两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- 制作jar包
1.打开cmd 2.通过cd切换到要打包的工程所在的bin目录(一定是bin目录) 运行jar -cvf aa.jar *.* jar是打包的命令 -cvf可以自行查看一下文档解释(jar -help ...
- C#操作WORD换行
appWord.ActiveDocument.Bookmarks[bookMark].Select(); Word.Selection wordSelection = appWord.ActiveDo ...
- webuploader限制只上传图片文件
// 如果上传的文件不为图片格式, 那么就提示"上传文件格式不正确" if (file.type!="image/jpg" && file.ty ...
- qt5.6 for android
尝试在qt下写android的程序.测试,没有编译成功. 在选项-android配置页面,提示有"Qt version for 3 architectures are missing&quo ...
- JAVA内容回顾(二)——面向对象(OOP)
1.类与对象 类:类指的是同种对象的抽象,看不见摸不着的.包含有属性与方法. 对象:是类的具体实现,看的见摸得着的东西. 类是对象的抽象,对象是类的具体实现. 2.访问修饰符 public:在项目的任 ...
- C++中Map常见用法以及按照value排序
今天做一个简单的算法题,居然用了1个小时,STL unordered_map用多了,没想到map这次派上了用场,这里记录一下: 算法题为 给一个字符串例如 abaaba,每连续两个字符组成一个子串 ...
- Linux镜像资源收集
1.企业 搜狐开源镜像站: http://mirrors.sohu.com/ 网易开源镜像站: http://mirrors.163.com/ 阿里开源镜像站: http://mirrors.aliy ...
- Selenium 中文API
Selenium 中文API 转自:http://blog.csdn.net/lh9529/article/details/3946567 概念 Selenium 通过命令进行驱动.Seleniu ...
- Java缓存框架
JBossCache/TreeCache JBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的改善性能.缓存数据被自动复制,让你轻松进行Jboss服务器之间的集群工作 ...
- Quickly Start Listener scripts
#!/usr/bin/python # # StartListener.py # Simple python script to start a Meterpreter Listener # Auto ...