2. 两数相加(中等)
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
解法一、模拟加法器
因为最开始不能确定l1和l2哪个长。当然也可以存在其中一个里,如果它先一步结束,再补next。记得判断最后的if carry != 0,末尾添1
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode list = new ListNode();
ListNode res = list;
while(l1!=null || l2!=null){
int num1 = 0,num2 = 0;
if(l1 != null){
num1 = l1.val;
l1 = l1.next;
}
if(l2 != null){
num2 = l2.val;
l2 = l2.next;
}
list.next = new ListNode();
list.next.val = (num1 + num2 + carry)%10;
list = list.next;
carry = (num1 + num2 + carry)/10;
}
if(carry != 0){
list.next = new ListNode(1);
}
return res.next;
}
}

445. 两数相加 II(中等)
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
解法一、栈
写得太赘余了
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int num1 = 0,num2 = 0,carry = 0;
ListNode temp = new ListNode();
ListNode pre = new ListNode();
pre.next = temp;
Deque<Integer> st1 = new ArrayDeque<>(),st2 = new ArrayDeque<>();
//记录数字
while(l1!=null){
st1.push(l1.val);
l1 = l1.next;
}
while(l2!=null){
st2.push(l2.val);
l2 = l2.next;
}
//相加,造新的res列表
while(!st1.isEmpty() || !st2.isEmpty() || carry != 0){
num1 = !st1.isEmpty() ? st1.pop():0;
num2 = !st2.isEmpty() ? st2.pop():0;
temp.val = (num1 + num2 + carry)%10;
carry = (num1 + num2 + carry)/10;
pre = new ListNode();
pre.next = temp;
temp = pre;
}
return pre.next;
}
}

官方题解里它对加节点方式的改进是这样,感觉会好很多
ListNode ans = null;
while (!stack1.isEmpty() || !stack2.isEmpty() || carry != 0) {
int a = stack1.isEmpty() ? 0 : stack1.pop();
int b = stack2.isEmpty() ? 0 : stack2.pop();
int cur = a + b + carry;
carry = cur / 10;
cur %= 10;
ListNode curnode = new ListNode(cur);
curnode.next = ans;
ans = curnode;
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/add-two-numbers-ii/solutions/200585/liang-shu-xiang-jia-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解法二、递归
l1和l2翻转、变成2题,res再翻转然后return的意思。这里保存一下递归的函数
private ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head; // 把下一个节点指向自己
head.next = null; // 断开指向下一个节点的连接,保证最终链表的末尾节点的 next 是空节点
return newHead;
}
作者:灵茶山艾府
链接:https://leetcode.cn/problems/add-two-numbers-ii/solutions/2328330/fan-zhuan-lian-biao-liang-shu-xiang-jia-okw6q/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
21. 合并两个有序链表(简单)
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
解法一、比较然后插入
一个空了的话直接插另一个就好
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode list = new ListNode(),res = list;
int num = 0;
while(list1 != null && list2 != null){
if(list1.val <= list2.val){
num = list1.val;
list1 = list1.next;
}else{
num = list2.val;
list2 = list2.next;
}
ListNode temp = new ListNode(num);
list.next = temp;
list = temp;
}
if(list1 == null)list.next = list2;
if(list2 == null)list.next = list1;
return res.next;
}
}

解法二、递归
。。太优雅了
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solutions/226408/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解法三、迭代
非常利落。。有点穿针引线的感觉,移换prev,指向需要的那个节点.而不是像解法一,存值然后开辟新的节点。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solutions/226408/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
23. 合并 K 个升序链表(困难)
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
解法一、循环迭代
参考了21的解法三。方法见注释,效率差得一笔
class Solution {
public static ListNode mergeKLists(ListNode[] lists) {
ListNode preHead = new ListNode(),prev = preHead;
int t = 0,len = lists.length;
int index = 0;
while(t != len){
ListNode temp = null;
int min = 10005;
//找出所有现有节点里最小那个
for(int i = 0;i < len;i++){
if(lists[i] != null){
if(lists[i].val < min){
min = lists[i].val;
temp = lists[i];
index = i;
}
}
}
//更新
if(lists[index] != null)lists[index] = lists[index].next;
if(lists[index] == null)t++;
if(prev != null)prev.next = temp;
prev = temp;
}
return preHead.next;
}
}

解法二、顺序合并
参考21,每次合并两个
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode ans = null;
for (int i = 0; i < lists.length; ++i) {
ans = mergeTwoLists(ans, lists[i]);
}
return ans;
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-k-sorted-lists/solutions/219756/he-bing-kge-pai-xu-lian-biao-by-leetcode-solutio-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解法三、分治合并
每次合并相邻两个
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length - 1);
}
public ListNode merge(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
if (l > r) {
return null;
}
int mid = (l + r) >> 1;
return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-k-sorted-lists/solutions/219756/he-bing-kge-pai-xu-lian-biao-by-leetcode-solutio-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
碎碎念
- 今天考察递归和分治的点比较多,感觉对链表那种滑动式检索(类指针)的形式更了解了。也熟悉了一些递归~
1804

被折叠的 条评论
为什么被折叠?



