CCI 2.5 链表整数求和
给定两个用链表表示的整数,每一个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。 示例 输入:(7-1-6) (5-9-2), 即 617 295. 输出:2-1-9, 即912. 进阶 假设这些数位是正向存放的,请
给定两个用链表表示的整数,每一个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。
示例
输入:(7->1->6) + (5->9->2), 即 617 + 295.
输出:2->1->9, 即912.
进阶
假设这些数位是正向存放的,请再做一遍。
示例
输入:(6->1->7) + (2->9->5), 即617 + 295.
输出:9->1->2, 即912.
**进阶的解法告诉我们,涉及单链表逆序的问题,可以借助栈来解决。
package test; import java.util.Stack; public class AddLinkedInt { //两个整数反向存放 //即,个位排在链表的首部 public Node addLinkedInt(Node l1, Node l2){ Node newHead = new Node(0); Node tail = newHead; int carry = 0; Node p1 = l1, p2 = l2; while(p1!=null || p2!=null){ int val1 = (p1==null)? 0 : p1.val; int val2 = (p2==null)? 0 : p2.val; int val = (val1+val2+carry)%10; carry = (val1+val2+carry)/10; tail.next = new Node(val); tail = tail.next; } if(carry != 0) tail.next = new Node(carry); return newHead.next; } //两个整数正向存放 //即,个位排在链表的末尾 public Node addLinkedInt(Node l1, Node l2){ //这里要借助栈来处理 Stack<integer> st1 = new Stack<integer>(); Stack<integer> st2 = new Stack<integer>(); Node p1=l1, p2=l2; //将两链表的数据分别压入栈中 while(p1 != null){ st1.push(p1.val); p1 = p1.next; } while(p2 != null){ st2.push(p2.val); p2 = p2.next; } //将结果相加入栈 Stack<integer> res = new Stack<integer>(); int carry=0; while( !st1.empty() || !st2.empty()){ int val1 = st1.empty() ? 0 : st1.pop(); int val2 = st2.empty() ? 0 : st2.pop(); res.push((val1+val2+carry)%10); carry = (val1+val2+carry)/10; } if(carry != 0)//注意进位的处理 res.push(carry); Node newHead = new Node(0); Node tail = newHead; while(!res.empty()){ tail.next = new Node(res.pop()); tail = tail.next; } return newHead.next; } } </integer></integer></integer></integer></integer></integer>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Python, tuples are immutable sequences that can store multiple elements of different types. They are often used to represent collections of related values. Tuple summation involves adding the corresponding elements of two or more tuples to produce a new tuple. However, in some scenarios, it may be necessary to calculate the absolute sum of elements instead of the traditional sum. In this blog post, we will explore how to perform absolute tuple sums in Python. Traditional Tuple Sum Before we delve into absolute tuple sum, let’s first understand how to do traditional tuple sum. Given two tuples of the same length, we can use a simple Python loop or list comprehension to calculate the sum of the corresponding elements −deftuple_sum(t1,t2):

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

The manipulation of date and time values is an important aspect of programming, and the Python language provides a useful built-in module for this called datetime. However, in some cases, you may need to convert a DateTime object to an integer value in order to perform specific operations or calculations. There are multiple ways to convert a DateTime to an integer in Python, each with its own advantages and disadvantages. In this article, we'll take a closer look at these methods and examine when each method is appropriate to use. After reading this article, you will have a complete understanding of how to efficiently convert DateTime objects to integers in Python and be able to choose the most appropriate method for your specific programming task. Method 1: Use timestamp

Sometimes, we often encounter counting problems in Word tables. Generally, when encountering such problems, most students will copy the Word table to Excel for calculation; some students will silently pick up the calculator. Calculate. Is there a quick way to calculate it? Of course there is, in fact the sum can also be calculated in Word. So, do you know how to do it? Today, let’s take a look together! Without further ado, friends in need should quickly collect it! Step details: 1. First, we open the Word software on the computer and open the document that needs to be processed. (As shown in the picture) 2. Next, we position the cursor on the cell where the summed value is located (as shown in the picture); then, we click [Menu Bar

The regular expressions for integers are: 1. Match positive integers: ^[1-9]\d*$; 2. Match negative integers: ^-[1-9]\d*$; 3. Match positive integers and negative integers :^-?\d+$; 4. Match non-zero integers: ^(0|[1-9]\d*)$; 5. Match integers (including zero): ^-?\d+$.

Given a singly linked list and a positive integer N as input. The goal is to find the Nth node from the end of the given list using recursion. If the input list has nodes a→b→c→d→e→f and N is 4, then the 4th node from the last will be c. We will first traverse until the last node in the list and when returning from the recursive (backtracking) increment count. When count equals N, a pointer to the current node is returned as the result. Let's look at various input and output scenarios for this - Input - List: -1→5→7→12→2→96→33N=3 Output − The Nth node from the last is: 2 Explanation − The third node is 2 . Input − List: -12→53→8→19→20→96→33N=8 Output – Node does not exist

Timestamp in PHP is an integer form representing time, usually the number of seconds that have passed since the first year of Unix (January 1, 1970 00:00:00 GMT). In programming, we often need to convert timestamps into other forms of integers. Here we will introduce how to convert PHP timestamps into integers, as well as specific code examples. In PHP, we can use the strtotime() function to convert the time string to a timestamp and then use date

How to use INET_ATON function in MySQL to convert IP address to integer? In network programming, the processing and storage of IP addresses are often involved. IP addresses are usually expressed in dotted-decimal form, such as 192.168.1.1. However, for some scenarios that require efficient storage and processing of IP addresses, it may be more convenient and efficient to convert the IP address to integer form. In the MySQL database, there is a built-in function called INET_ATON
