当前位置:网站首页>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
边栏推荐
- 【Hot100】23. Merge K ascending linked lists
- 分享几个图床网址,便于大家分享图片
- Burp install license key not recognized
- At compilation environment setup -win
- Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
- CS5268完美代替AG9321MCQ Typec多合一扩展坞方案
- The metamask method is used to obtain account information
- 【871. 最低加油次数】
- JDBC | Chapter 4: transaction commit and rollback
- Backpack template
猜你喜欢
B-end e-commerce - reverse order process
Summary of interview experience, escort your offer, full of knowledge points
【每日一题】241. 为运算表达式设计优先级
Solution to blue screen after installing TIA botu V17 in notebook
The first of the classic quotations of correspondents is heartbreaking
通信人的经典语录,第一条就扎心了……
Driverless learning (4): Bayesian filtering
In depth understanding of modern web browsers (I)
Sometimes only one line of statements are queried, and the execution is slow
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
随机推荐
Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project
接口测试到底怎么做?看完这篇文章就能清晰明了
面试经验总结,为你的offer保驾护航,满满的知识点
【Hot100】22. 括号生成
Research and Analysis on the current situation of China's clamping device market and forecast report on its development prospect
勵志!大凉山小夥全獎直博!論文致謝看哭網友
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
Share several map bed websites for everyone to share pictures
In depth understanding of modern web browsers (I)
2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
证券如何在线开户?手机开户是安全么?
Review of the latest 2022 research on "deep learning methods for industrial defect detection"
Use graalvm native image to quickly expose jar code as a native shared library
[daily question] 241 Design priorities for operational expressions
Detailed upgrade process of AWS eks
通信人的经典语录,第一条就扎心了……
笔记本安装TIA博途V17后出现蓝屏的解决办法
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)
Overview of browser caching mechanism
Driverless learning (4): Bayesian filtering