当前位置:网站首页>Add two numbers of leetcode
Add two numbers of leetcode
2022-07-02 20:35:00 【wx58c8fa5d0b356】
List of articles
- Example
LeetCode Add two numbers
Title Description
Two are given. Non empty The list of is used to represent two non negative integers . among , Their respective digits are based on The reverse Stored in , And each of their nodes can only store a Numbers .
If , Let's add the two numbers together , A new list will be returned to represent their sum .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Example
Input :(2 -> 4 -> 3) + (5 -> 6 -> 4)
Output :7 -> 0 -> 8
reason :342 + 465 = 807
Problem solving
Code
/**
* @program: algorithm_code
* @description: Algorithm problem 2
* @author: YangHang
* @create: 2019-08-25 21:57
**/
public class Num2_AddTwoNumbers {
public static void main(String[] args) {
// Assembly data
ListNode list11 = new ListNode(9);
ListNode list12 = new ListNode(9);
list11.next = list12;
ListNode list21 = new ListNode(9);
printLinkedList(addTwoNumbers(list11, list21));
}
public static ListNode addTwoNumbers(ListNode list11, ListNode list21) {
ListNode temp1 = list11;
ListNode temp2 = list21;
// Carry or not
boolean flag = false;
ListNode result = new ListNode(-1);
ListNode temp = result;
while (temp1 != null || temp2 != null) {
int num1 = 0;
int num2 = 0;
if (temp1 != null) {
num1 = temp1.val;
}
if (temp2 != null) {
num2 = temp2.val;
}
if (flag) {
if (num1 + num2 + 1 >= 10) {
temp.next = new ListNode(num1 + num2 + 1 - 10);
flag = true;
} else {
temp.next = new ListNode(num1 + num2 + 1);
flag = false;
}
} else {
if (num1 + num2 >= 10) {
temp.next = new ListNode(num1 + num2 - 10);
flag = true;
} else {
temp.next = new ListNode(num1 + num2);
flag = false;
}
}
temp = temp.next;
if (temp1 != null) {
temp1 = temp1.next;
}
if (temp2 != null) {
temp2 = temp2.next;
}
}
if (flag) {
temp.next = new ListNode(1);
}
return result.next;
}
public static void printLinkedList(ListNode listNode) {
ListNode temp = listNode;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
System.out.println();
}
}
// List class
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
leetcode Exhibition

边栏推荐
- [Chongqing Guangdong education] reference materials for labor education of college students in Nanjing University
- 勵志!大凉山小夥全獎直博!論文致謝看哭網友
- [译]深入了解现代web浏览器(一)
- at编译环境搭建-win
- [cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
- 在网上炒股开户安全吗?我是新手,还请指导
- [QT] QPushButton creation
- 自动化制作视频
- 【Kubernetes系列】kubeadm reset初始化前后空间、内存使用情况对比
- Research Report on the overall scale, major manufacturers, major regions, products and applications of outdoor vacuum circuit breakers in the global market in 2022
猜你喜欢

An analysis of the past and present life of the meta universe

【Hot100】21. Merge two ordered linked lists

Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..

Second hand housing data analysis and prediction system

Properties of expectation and variance

AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion

测试人员如何做不漏测?这7点就够了

Review of the latest 2022 research on "deep learning methods for industrial defect detection"

数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
随机推荐
Activation function - relu vs sigmoid
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
Overview of browser caching mechanism
upload-labs
Jetson XAVIER NX上ResUnet-TensorRT8.2速度與顯存記錄錶(後續不斷補充)
面试经验总结,为你的offer保驾护航,满满的知识点
Summary of interview experience, escort your offer, full of knowledge points
Web3js method to obtain account information and balance
Solution to blue screen after installing TIA botu V17 in notebook
[JS] get the search parameters of URL in hash mode
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
Second hand housing data analysis and prediction system
Interested parties add me for private chat
Is it safe to buy funds on securities accounts? Where can I buy funds
[kubernetes series] comparison of space and memory usage before and after kubedm reset initialization
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
C language linked list -- to be added
数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
API documentation tool knife4j usage details