面试题:Add Two Numbers(模拟单链表)
题干:
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
注:
题目中链表的定义为
public class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
分析:
题干中链表中的数是倒序存储的,题干中给的例子其实是:342+465=807。
我们定义一个节点dummyHead来指向结果的头结点,然后定义三个节点p,q,curr分别用来记录l1当前节点,l2当前节点,结果当前节点。
定义int carry来记录是否有进位。
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null)
p = p.next;
if (q != null)
q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
面试题:Add Two Numbers(模拟单链表)的更多相关文章
- PHP模拟单链表的数据结构
<?php /*** * 单链表 */ //节点,下标,节点名称,下一个节点的地址 class Node { public $id; public $name; public $next; pu ...
- 2.Add Two Numbers-两个单链表相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- 2. Add Two Numbers(2个链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- python算法双指针问题:使用列表和数组模拟单链表
这个很多基础算法,python已内部实现了. 所以,要想自己实现链表这些功能时, 反而需要自己来构造链表的数据结构. 当然,这是python灵活之处, 也是python性能表达不如意的来源. valu ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
- Add Two Numbers ,使用链表参数
# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x sel ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- java实现单链表的增删改以及排序
使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...
随机推荐
- [洛谷P3807]【模板】卢卡斯定理
题目大意:给你$n,m,p(p \in \rm prime)$,求出$C_{n + m}^m\bmod p(可能p\leqslant n,m)$ 题解:卢卡斯$Lucas$定理,$C_B^A\bmod ...
- 【CZY选讲·Hja的棋盘】
题目描述 Hja特别有钱,他买了一个×的棋盘,然后Yjq到这个棋盘来搞事.一开始所有格子都是白的,Yjq进行次行操作次列操作,所谓一次操作,是将对应的行列上的所有格子颜色取反.现在Yjq希望搞事之后 ...
- Codeforces Round #357 (Div. 2) B
B. Economy Game time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #516 (Div. 2)D. Labyrinth
D. Labyrinth 题目链接:https://codeforces.com/contest/1064/problem/D 题意: 给出一个n*m的矩阵以及人物的起点,并且给出x,y,分别代表这个 ...
- POJ -1679(次小生成树)模板
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions:34617 Accepted: 12637 D ...
- HDU 3853 LOOP (概率DP求期望)
D - LOOPS Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- 搜索水题四连发_C++
特别声明:以下题目有部分为原创题,涉及版权问题,不得转载,违者追究 法律责任! 话说这是一套神题,只有你想不到,没有你做不到 题目更正后比 Pascal 跑得还快哈~ 一道特别裸,但是特别坑的搜索题 ...
- 战斗机的祈雨仪式(NOIP模拟赛Round 7)
[问题描述] 炎炎夏日,如果没有一场大雨怎么才能尽兴?秋之国的人民准备了一场祈雨仪式.战斗机由于拥有操纵雷电的能力,所以也加入了其中,为此,她进行了一番准备. 战斗机需要给自己的Spear of Lo ...
- bind 简单配置dns
一. 安装apt-get install bind9 apt-get install bind9-host dnsutils apt-get install bind9-doc 二.修改本机配置我们要 ...
- JMeter乱码问题的解决
一.JMeter返回数据是乱码 解决办法是: 在JMeter安装路径的bin目录下,以记事本打开文件jmeter.properties, 找到Sampleresult.default.encoding ...