In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Java how to achieve the addition of two numbers", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to achieve the addition of two numbers" this article.
Topic description
Two non-empty linked lists are given to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each of their nodes can only store one digit.
If we add these two numbers together, we will return a new linked list to represent their sum.
You can assume that neither of these numbers will start with 0 except for the number 0.
Example:
Input: (2-> 4-> 3) + (5-> 6-> 4) output: 7-> 0-> 8 reasons: 342 + 465 = 807 solution
Tags: linked list
Think of two linked lists as traversing of the same length. If a linked list is short, add 0 in front of it, for example, 987 + 23 = 987 + 023 = 1010
The carry problem of the previous bit needs to be considered when each bit is calculated, and the carry value also needs to be updated after the current bit calculation is finished.
If the carry value is 1 after the two linked lists are all traversed, node 1 is added at the front of the new linked list.
Tip: for linked list problems, when the return result is a header, you usually need to initialize a pre-pointer pre, and the next node of the pointer points to the real header head. The purpose of using advance pointers is that there are no node values available during list initialization, and the linked list construction process requires pointer movement, which results in the loss of the head pointer and the inability to return the result.
Code / * 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 pre = new ListNode (0); ListNode cur = pre; int carry = 0; while (L1! = null | | L2! = null) {int x = L1 = null? 0: l1.val Int y = L2 = = null? 0: L2.val.int sum = x + y + carry; carry = sum / 10; sum = sum% 10; cur.next = new ListNode (sum)
Cur = cur.next; if (L1! = null) L1 = L1.next; if (L2! = null) L2 = l2.next;} if (carry = = 1) {cur.next = new ListNode (carry);} return pre.next;}}
The above is all the content of the article "how to add two numbers in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.