本系列为本人leetcode刷题之路2.0,将相关思考记录在此博客。欢迎批评指正。
题目理解
https://leetcode-cn.com/problems/add-two-numbers/
考察链表知识:
两个非空链表表示两个非负整数,数字按照逆序存储(342存储为【2,4,3】)。
故要将两个数相加即为:
个位、十位、百位依次相加 ==>链表从左至右依次对应相加,满十进一。
结果以相同形式返回一个表示和的链表(也是逆序存储个十百)。
注意两种情况:
l1:243, l2:363,结果是507。注意满十进一情况
l1:[999], l2:[99], 结果是891。注意长度不等情况(这个还有进一情况)
最好实操下演算过程,有助于理解代码
解题思路
①暴力法
②递归法
解法1:暴力法
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
res = ListNode() #因为上面有默认参数val=0,next=None
cur = res
digit = 0
while l1 or l2:
total = digit
total += (l1.val if l1 else 0) + (l2.val if l2 else 0)
if l1:
l1 = l1.next
if l2:
l2 = l2.next #这块还是要注意有可能l1或者l2为null
digit = total // 10
cur.next = ListNode(total % 10)
cur = cur.next
if digit != 0:
cur.next = ListNode(digit) # 注意这点,如999+99,顺序加到第三位满十进一还要加在第三位为1。
return res.next
总结:注意这个程序的三个注释点
java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode res = new ListNode();
ListNode cur = res;
int digit = 0;
while (l1 != null || l2 != null) { //要注意java的{}阿,不要写成python的:冒号了
int total = digit;
if (l1 != null) {
total += l1.val;
l1 = l1.next; //注意l1和l2都要往后移一位
}
if (l2 != null) {
total += l2.val;
l2 = l2.next;
}
digit = total / 10; //java除号
cur.next = new ListNode(total % 10);
cur = cur.next;
}
if (digit!=0)
cur.next= new ListNode(digit);
return res.next;
}
}
疑问:对java链表创建过程有疑惑,如上的ListNode类。
总结:关注程序中的注释。
解法2:递归法
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
total = l1.val + l2.val #注意python的写法 l1.val....不是l1->val
digit = total // 10
remain = total % 10
res = ListNode(remain)
if l1.next or l2.next or digit: #递归写法。应该没有while for之类的循环体。
l1 = l1.next if l1.next else ListNode(0)
l2 = l2.next if l2.next else ListNode(0)
l1.val += digit
res.next = self.addTwoNumbers(l1, l2)
return res
总结:为啥递归做法与暴力解法复杂度差不多?
关于递归,学学忘忘,不擅长运用,需要加强。
java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int total = l1.val + l2.val;
int digit = total / 10;
ListNode res = new ListNode(total % 10);
if (l1.next != null || l2.next != null || digit != 0) { // 注意还有digit这个条件判断
l1 = l1.next != null ? l1.next : new ListNode(0); //注意l1和l2往后移动怎么移
l2 = l2.next != null ? l2.next : new ListNode(0);
l1.val += digit;
res.next = addTwoNumbers(l1, l2);
}
return res;
}
}
总结:注意程序中的注释。