当前位置:网站首页>TOP2两数相加
TOP2两数相加
2022-08-11 05:35:00 【geekmice】
题目描述
你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头
如图所示
提示:
每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode root = new ListNode(0);
ListNode cursor = root;
int carry = 0;
while(l1 != null || l2 != null || carry != 0) {
int l1Val = l1 != null ? l1.val : 0;
int l2Val = l2 != null ? l2.val : 0;
int sumVal = l1Val + l2Val + carry;
carry = sumVal / 10;
ListNode sumNode = new ListNode(sumVal % 10);
cursor.next = sumNode;
cursor = sumNode;
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}
return root.next;
}
}
参考地址:https://leetcode.cn/problems/add-two-numbers/comments/
边栏推荐
- SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
- 文本三剑客——grep过滤
- SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
- 防火墙-0-管理地址
- Solve the problem that port 8080 is occupied
- 文本三剑客——awk 截取+过滤+统计
- buildroot嵌入式文件系统中vi显示行号
- cloudreve使用体验
- No threat of science and technology - TVD vulnerability information daily - 2022-7-21
- WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
猜你喜欢
随机推荐
解决win10安装portal v13/v15要求反复重启问题。
arcgis填坑_1
AUTOMATION DAY07( Ansible Vault 、 普通用户使用ansible)
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
buildroot setup dhcp
Project Notes - Random 2
(1) Software testing theory (0 basic understanding of basic knowledge)
lvm 多盘挂载,合并使用
查看可执行文件依赖的库ldd
(2) Software Testing Theory (*Key Use Case Method Writing)
HCIP OSPF/MGRE综合实验
CLUSTER DAY01(集群及LVS简介 、 LVS-NAT集群 、 LVS-DR集群)
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
【LeetCode】2034. 股票价格波动(思路+题解)双map
文本三剑客——sed 修改、替换
升级到Window11体验
华为防火墙-6
slurm集群搭建
lvm multi-disk mount, combined use
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-30









