当前位置:网站首页>Bm11 list addition (II)
Bm11 list addition (II)
2022-07-28 19:10:00 【Love life and programming a】

Ideas: First reverse the two linked lists, then add the values in turn and establish a new node to connect to the result linked list , Finally, reverse the linked list of results .
solution 1
public class Solution {
/** * * @param head1 ListNode class * @param head2 ListNode class * @return ListNode class */
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
if(head1 == null && head2 == null){
return null;
}
if(head1 == null){
return head2;
}
if(head2 == null){
return head1;
}
// 1: Reverse two linked lists
ListNode p1 = reverse(head1);
ListNode p2 = reverse(head2);
int approve = 0;
int val = 0;
ListNode ret = new ListNode(1);
ListNode cur = ret;
// 2: If the corresponding two nodes are not empty, add and record the carry , Finally, create nodes with the results and string them in the result linked list
while(p1 != null && p2 != null){
val = (p1.val + p2.val + approve) % 10;
approve = (p1.val + p2.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p1 = p1.next;
p2 = p2.next;
}
while(p1 != null){
val = (p1.val + approve) % 10;
approve = (p1.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p1 = p1.next;
}
while(p2 != null){
val = (p2.val + approve) % 10;
approve = (p2.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p2 = p2.next;
}
// 3: Add a value of 1 The node of
if(approve == 1){
cur.next = new ListNode(1);
}
// 4: Reverse the final result of construction again
return reverse(ret.next);
}
public ListNode reverse(ListNode head){
ListNode cur = head;
ListNode prev = null;
while(cur != null){
ListNode nextNode = cur.next;
cur.next = prev;
prev = cur;
cur = nextNode;
}
return prev;
}
}
solution 2
public class Solution {
/** * * @param head1 ListNode class * @param head2 ListNode class * @return ListNode class */
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
// 1: Create two stacks and put the two linked lists into the stack respectively
Stack<Integer> stack1=new Stack();
Stack<Integer> stack2=new Stack();
int cnt=0;
int sum=0;
ListNode dummy=new ListNode(0);
while(head1!=null){
stack1.add(head1.val);
head1=head1.next;
}
while(head2!=null){
stack2.add(head2.val);
head2=head2.next;
}
// 2: Take out the elements of the two stacks at the same time and add them , Finally, connect to the result linked list
while(!stack1.isEmpty()||!stack2.isEmpty()||cnt>0){
int c=!stack1.isEmpty()?stack1.pop():0;
int d=!stack2.isEmpty()?stack2.pop():0;
sum=(c+d+cnt)%10;
cnt=(c+d+cnt)/10;
ListNode node=new ListNode(sum);
// Here we use the method of head insertion to connect, which saves the process of final reversal
node.next=dummy.next;
dummy.next=node;
}
return dummy.next;
}
}
边栏推荐
- Structure and working principle of thyristor
- Three minutes to understand, come to new media
- ECS 5 workflow
- Is it useful to learn software testing?
- Four years later, Debian finally recaptured the "debian.community" domain name!
- New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
- 历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
- When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class
- Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
- 视频融合云服务EasyCVR平台白名单功能如何使用?
猜你喜欢

The ever-changing pointer ----- C language

优麒麟系统安装BeyondComare

2、 Uni app login function page Jump

1、 My first wechat applet

Self cultivation of Electronic Engineers - when a project is developed

11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?

Redis cache avalanche, penetration, breakdown, bloom filter, detailed explanation of distributed lock

6-20漏洞利用-proftpd测试

What is the future of software testing? How to learn?

软件测试开发基础|测开中的几个工具开发实战
随机推荐
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
My creation anniversary -- July 25th, 2022
BM16 删除有序链表中重复的元素-II
Mongodb initialization
From Bayesian filter to Kalman filter (zero)
Special Lecture 6 tree DP learning experience (long-term update)
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version
2022杭电多校第二场1011 DOS Card(线段树)
OAI L3 and L2 interface analysis
More loading in applets (i.e. list paging)
Why app uses JSON protocol to interact with server: serialization related knowledge
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
Can the training software test be employed
QT with line encoding output cout
Mongodb database replication table
CTR click through rate prediction practice project of advertising recommendation!
The ever-changing pointer ----- C language
C language (high-level) character function and string function + Exercise
UE4.25 Slate源码解读