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 ...
随机推荐
- 关于CGI、FastCGI和PHP-FPM的关系
1.CGI是干嘛的? CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- 绝对路径&相对路径
被绝对路径和相对路径搞疯了,好多地方不一样,从今天开始,记录下来每次关于这个问题的记录,以备查用 css文件里: 绝对路径:以"/"开头,表示从项目的根目录开始
- 用户输入密码隐藏之getpass的使用
有的时候,比如商城登录的时候,我希望输入的时候我的密码不为明文,如何实现呢? 这里就需要利用getpass模块中的getpass方法.注意,需要在linux上或者windows下运行,在pycharm ...
- bash脚本退出代码解释
Exit Codes With Special Meanings Table E-1. Reserved Exit Codes Exit Code Number Meaning Example Com ...
- C3P0连接池参数详解
<c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> < ...
- Node.js入门 NPM
参考一 Node入门 七天学会NodeJS Node.js v4.2.4 手册 & 文档 Node.js 教程 node.js摸石头系列 从零开始学习node.js What is ...
- Python基础篇-day7
本节目录-面向对象1 类介绍1.1 面向对象oo特征1.2 类的特性1.3 创建与调用 1.3.1 基本结构 1.3.2 结构说明 1.3.3 对外部提供只读访问接口 1.3.4 析构方法2 继承2. ...
- SSLPinning 延伸
AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程大致是,客户端和服务端建立一个连接,服务端返回一个证书, ...
- 把Excel工作簿的每个工作表提取出来保存为新工作簿
平台:MS office 2010 任务:有个excel工作簿,其中有上百个工作表,要求把每一个工作表全部保存为新工作簿,如果一个一个复制出来太傻了,可以用excel自带的VB解决. 方法:打开工作簿 ...
- LEK-Introduction
LEK - logstash + elasticsearch + Kibana Elasticsearch, Logstash, and Kibana — designed to take data ...