当前位置:网站首页>BM11 链表相加(二)
BM11 链表相加(二)
2022-07-28 17:13:00 【爱生活爱编程a】

思路:先反转两个链表然后依次进行值相加并建立新节点连接到结果链表中,最后反转结果链表即可.
解法1
public class Solution {
/** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */
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: 反转两个链表
ListNode p1 = reverse(head1);
ListNode p2 = reverse(head2);
int approve = 0;
int val = 0;
ListNode ret = new ListNode(1);
ListNode cur = ret;
// 2: 若对应的两个节点都不为空则相加并记录进位,最后用结果创建节点并串在结果链表中
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: 算到最后若还有进位则添加一个值为1的节点
if(approve == 1){
cur.next = new ListNode(1);
}
// 4: 再次反转构造最终结果
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;
}
}
解法2
public class Solution {
/** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
// 1: 创建两个栈并将两个链表分别放入栈中
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: 同时取出两个栈的元素并相加,最后连接到结果链表中
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);
// 这里使用头插的方式进行连接省去了最后再反转的过程
node.next=dummy.next;
dummy.next=node;
}
return dummy.next;
}
}
边栏推荐
- C and SQL mixed programming, vs need to download what things
- kotlin:Nothing
- Is the software testing training institution reliable?
- What does real HTAP mean to users and developers?
- Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.
- JVM four reference types
- 4、 Interface requests data to update input information interactively
- 2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
- The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.
- Xiaobai must see the development route of software testing
猜你喜欢

When the new version of easycvr is linked at the same level, the subordinate platform passes up the cause analysis of the incomplete display of the hierarchical directory

112. Use the self-developed proxy server to solve the cross domain access error encountered when uploading files by SAP ui5 fileuploader

4 年后,Debian 终夺回“debian.community”域名!

我的创作纪念日 -- 2022年7月25日

What if svchost.exe of win11 system has been downloading?

N32 replaces STM32. Don't ignore these details!

4、 Interface requests data to update input information interactively

视频融合云服务EasyCVR平台白名单功能如何使用?
![[GXYCTF2019]StrongestMind](/img/f4/61932548e0c7dd60d790d31fb5b96b.png)
[GXYCTF2019]StrongestMind

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
随机推荐
LeetCode_ 1137_ Nth teponacci number
How much is software testing training generally?
Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
Introduction and advanced level of MySQL (I)
How new people get started learning software testing
kotlin:Nothing
Meta Q2 earnings: revenue fell for the first time, and metaverse will compete with apple
What if svchost.exe of win11 system has been downloading?
kotlin:out in
The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.
Lookup - lookup of sequential table and ordered table
Overview and working principle of single chip microcomputer crystal oscillator
How long does software testing training take?
2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
Mongodb database replication table
[GXYCTF2019]StrongestMind
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
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
Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version