Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the addition of two numbers in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve the addition of two numbers in Java". In the daily operation, I believe that many people have doubts about how to achieve the addition of two numbers in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve the addition of two numbers in Java". Next, please follow the editor to study!

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2-> 4-> 3) + (5-> 6-> 4) Output: 7-> 0-> 8 342 465 = 807

Topic description: enter two non-empty single linked lists, the value of each node of the linked list is a 1-digit integer, the two linked lists are a large integer in reverse order, and find the linked list value of the sum of the integers represented by the two linked lists.

Train of thought: 1. Get two numbers and add them to create a new list write. two。 Everyone wants to take extra time to consider the carry mark.

Def addTwoNumbers (L1) L2): move = 0 while L1: numOfL1 = L1.val * 10**move move + = 1 L1 = L1.next move = 0 while L2: numOfL2 = L2.val * 10**move move + = 1 L2 = L2.next final = numOfL1 + numOfL2 h = m = ListNode (0) if not final: return h while final: m.next = ListNode (final% 10) final = final/10 m = m.next # m.next default is empty return h.next# function with carry flag bit is good def addTwoNumbers (L1 L2): if L1 is None: return L2 if L2 is None: return L1 tmp = ListNode (0) # final res = tmp flag = 0 # carry marker while L1 or L2: tmpsum = 0 if L1: tmpsum = L1.val L1 = L1.next if L2: tmpsum + = L2.val L2 = L2.next tmpres = (tmpsum + flag)% 10) # remainder flag = ((tmpsum + flag) / / 10) # carry Res.next = ListNode (tmpres) res = res.next if flag: res.next = ListNode (1) res = tmp.next return res so far The study on "how to add two numbers in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report