当前位置:网站首页>【LeetCode】Add the linked list with carry
【LeetCode】Add the linked list with carry
2022-08-02 05:03:00 【EvilChou】


/*** Definition for singly-linked list.* public class ListNode {* int val;*ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode pre = new ListNode(0); //Open up a new memory space, pre points to the memory space, and pre is the head node of the result listListNode cur = pre; //Define a movable pointer that also points to this memory space, and subsequent addition of nodes depends on this pointerint carry = 0; //carrywhile(l1!=null || l2!=null){int x = l1 != null ? l1.val : 0; //Get the value at the corresponding position, if a position is empty, assign it to 0int y = l2 != null ? l2.val : 0;int sum = x + y + carry; //Add the value at the corresponding position of l1 and l2 and the value of the carrycarry = sum / 10; //Get the carry after each additionsum = sum % 10; //Get the value after the carrycur.next = new ListNode(sum); //Create a new node, the value of the node is the value after adding the carry, and the pointer of the resulting linked list points to the nodecur = cur.next; //The result linked list pointer moves forward, the pre head pointer does not moveif(l1!= null){l1 = l1.next;}if(l2!= null){l2 = l2.next;}}if(carry == 1){cur.next = new ListNode(carry); //If there is a carry after the last addition (the carry value must be 1), create a new node and save the node}return pre.next;//Return the result linked list, because pre points to the head pointer of the linked list, the value at the head pointer is 0, and the next node of the head pointer is the first node of the result linked list}}Open up a new linked list space ListNode pre = new ListNode(0) pre is the head node pointer


The last return is return pre.next, pre.next points to the first node of the result list

Do not use the head node, directly return the result list method

边栏推荐
- 剑指Offer 32.Ⅱ从上到下打印二叉树
- rosdep update失败解决办法(亲测有效)
- Pylon CLI 低成本的本地环境管控工具应用实例
- 剑指Offer 47.礼物的最大值 动态规划
- STM32 CAN 介绍以及相关配置
- STM32F4 CAN 配置注意的细节问题
- 【nRF24L01 connects with Arduino to realize wireless communication】
- bluez5.50+pulseaudio实现蓝牙音响音频播放
- Basic IO (below): soft and hard links and dynamic and static libraries
- 改变文件的扩展名
猜你喜欢

【LeetCode】合并

Comparison between Boda Industrial Cloud and Alibaba Cloud

Personal image bed construction based on Alibaba Cloud OSS+PicGo

Lightly:新一代的C语言IDE

AD实战篇

联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料

GM7150 CVBS转BT656视频解码芯片详细内容及设计要求

AD8307对数检波器

Process (in): process state, process address space

Pylon CLI 低成本的本地环境管控工具应用实例
随机推荐
vector的使用和模拟实现:
联阳IT66121FN提供SDI转HDMI方案分享
增量编译技术在Lightly中的实践
机械臂运动学解析
Compatible with C51 and STM32 Keil5 installation method
开源日志库 [log4c] 使用
电子密码锁_毕设‘指导’
引擎开发日志:重构骨骼动画系统
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
【MQ-3 Alcohol Detector and Arduino Detect Alcohol】
MQ-5 combustible gas sensor interface with Arduino
Kinematics Analysis of Robot Arm
剑指Offer 36.二叉搜索树与双向链表 中序遍历
关于IIC SDA毛刺的那些事
分割回文串 DP+回溯 (LeetCode-131)
The use and simulation of vector implementation:
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
剑指Offer 13.机器人的运动范围 深度优先遍历
GM8775C规格书,MIPI转LVDS,MIPI转双路LVDS分享