当前位置:网站首页>Linked list: adding numbers in the linked list
Linked list: adding numbers in the linked list
2022-06-13 02:44:00 【Zeng Qiang】
List of articles
subject
Their thinking
- Method 1 :
The length of two linked lists may be different , Convert the linked list nodes into integers , Then add integers , Finally, the sum obtained is reduced to a linked list . But when you add integers , Integer overflow may occur . So this method is not feasible .
- Method 2 :
Reverse a linked list , Align the two tails of the original linked list , That is, we can start with single digits , Add digit by digit . Finally, restore the inverted linked list .
The specific code is as follows :
Code
/** * 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) {
l1 = reverseList(l1);
l2 = reverseList(l2);
return reverseList(addReversed(l1, l2));
}
private ListNode addReversed(ListNode reverseL1, ListNode reverseL2) {
int carry = 0;
ListNode dummy = new ListNode(0);
ListNode sumNode = dummy;
while(reverseL1 != null || reverseL2 != null) {
int sum = (reverseL1 == null ? 0 : reverseL1.val) + (reverseL2 == null ? 0 : reverseL2.val) + carry;
carry = sum >= 10 ? 1 : 0;
sum = sum >= 10 ? sum - 10 : sum;
ListNode newNode = new ListNode(sum);
sumNode.next = newNode;
sumNode = sumNode.next;
reverseL1 = reverseL1 == null ? null : reverseL1.next;
reverseL2 = reverseL2 == null ? null : reverseL2.next;
}
if(carry > 0) {
sumNode.next = new ListNode(carry);
}
return dummy.next;
}
private ListNode reverseList(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
summary
Add the numbers in the linked list , You can use the characteristics of the reverse linked list , Add from the single digits of the number .
边栏推荐
- nn. Conv2d and nn Convtranspose2d differences
- [deep learning] fast Reid tutorial
- Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
- 重定向设置参数-RedirectAttributes
- Leetcode 450. Delete node in binary search tree [binary search tree]
- Ijkplayer source code ---setdatasource
- Welcome to blog navigation
- [reading papers] deep learning face representation from predicting 10000 classes. deepID
- L1 regularization and its sparsity
- C # illustrated tutorial (Fourth Edition) chapter7-7.2 accessing inherited members
猜你喜欢
![[data analysis and visualization] key points of data drawing 9- color selection](/img/76/0d707b3d2446b5cd8002cbc2834c5c.jpg)
[data analysis and visualization] key points of data drawing 9- color selection

Basic principle of bilateral filtering
![[reading papers] comparison of deeplobv1-v3 series, brief review](/img/80/714b8e5b2ad31b0a1a0b8320a3c714.jpg)
[reading papers] comparison of deeplobv1-v3 series, brief review

在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容

Branch and bound method, example sorting

Useful websites for writing papers and studying at ordinary times

Use of OpenCV 12 findcircuits and drawcircuits

Hstack, vstack and dstack in numpy
![[data analysis and visualization] key points of data mapping 7- over mapping](/img/ae/d4e251b37ec4857c99f738ca981092.jpg)
[data analysis and visualization] key points of data mapping 7- over mapping

Principle and steps of principal component analysis (PCA)
随机推荐
Introduction and download of common data sets for in-depth learning (with network disk link)
Opencvsharp4 handwriting recognition
Hstack, vstack and dstack in numpy
Mp4 playback
如何挑选基金产品?什么样的基金是好基金?
Graduation project - campus old thing recycling system based on stm32
数仓笔记|针对客户维度建模需要关注的5个因素
Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
Vant realizes the adaptation of mobile terminal
[data analysis and visualization] key points of data drawing 4- problems of pie chart
[reading papers] visual convolution zfnet
專業的數據庫管理軟件:Valentina Studio Pro for Mac
SANs证书生成
遍历数组,删除某元素,直到删除为止
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion
[reading some papers] introducing deep learning into the public horizon alexnet
Android lightweight cache processing
05 tabBar导航栏功能
too old resource version,Code:410
Use of OpenCV 12 findcircuits and drawcircuits