題目來源于 LeetCode 上第 2 號(hào)問題:兩數(shù)相加李丰。題目難度為 Medium圃阳,目前通過率為 33.9% 。
題目描述
給出兩個(gè) 非空 的鏈表用來表示兩個(gè)非負(fù)的整數(shù)罢荡。其中憨募,它們各自的位數(shù)是按照 逆序 的方式存儲(chǔ)的紧索,并且它們的每個(gè)節(jié)點(diǎn)只能存儲(chǔ) 一位 數(shù)字。
如果菜谣,我們將這兩個(gè)數(shù)相加起來珠漂,則會(huì)返回一個(gè)新的鏈表來表示它們的和。
您可以假設(shè)除了數(shù)字 0 之外尾膊,這兩個(gè)數(shù)都不會(huì)以 0 開頭媳危。
<font color=#FF000 >題目難度: ★★, 中等</font>
image.png
示例 1:
輸入:l1 = [2,4,3], l2 = [5,6,4]
輸出:[7,0,8]
解釋:342 + 465 = 807.
示例 2:
輸入:l1 = [0], l2 = [0]
輸出:[0]
示例 3:
輸入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
輸出:[8,9,9,9,0,0,0,1]
提示:
每個(gè)鏈表中的節(jié)點(diǎn)數(shù)在范圍 [1, 100] 內(nèi)
0 <= Node.val <= 9
題目數(shù)據(jù)保證列表表示的數(shù)字不含前導(dǎo)零
題目解析
設(shè)立一個(gè)表示進(jìn)位的變量carried
,建立一個(gè)新鏈表冈敛,把輸入的兩個(gè)鏈表從頭往后同時(shí)處理待笑,每兩個(gè)相加,將結(jié)果加上carried
后的值作為一個(gè)新節(jié)點(diǎn)到新鏈表后面抓谴。
代碼實(shí)現(xiàn)
tips: 以下代碼是使用Go代碼實(shí)現(xiàn)的不同解法, 文章最后可以看C++暮蹂、C寞缝、Java、Python實(shí)現(xiàn)
1仰泻、循環(huán)遍歷, 進(jìn)行求和
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var carry int
resultList := &ListNode{}
current := resultList
for{
if l1 == nil && l2 == nil && carry == 0{
break
}
if l1 != nil{
carry += (*l1).Val
l1 = l1.Next
}
if l2 != nil{
carry += (*l2).Val
l2 = l2.Next
}
node := ListNode{}
if carry <= 9{
node = ListNode{
Val: carry,
}
carry = 0
}else{
node = ListNode{
Val: carry - 10,
}
carry = 1
}
current.Next = &node
current = &node
}
return resultList.Next
}
執(zhí)行結(jié)果
image.png
2荆陆、改進(jìn)方法, <font color=#FF000 >如果l1、l2長度差別很大, 就可以直接利用偏長鏈表后面的部分, 避免重復(fù)new Node節(jié)點(diǎn)</font>集侯。
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var carry int
resultList := &ListNode{}
current := resultList
for{
node := ListNode{}
if l1 == nil && l2 == nil && carry == 0{
break
}
if l1 != nil{
if l2 == nil && carry == 0{
current.Next = l1
break
}else{
carry += (*l1).Val
l1 = l1.Next
}
}
if l2 != nil{
if l1 == nil && carry == 0{
current.Next = l2
break
}else{
carry += (*l2).Val
l2 = l2.Next
}
}
if carry <= 9{
node = ListNode{
Val: carry,
}
carry = 0
}else{
node = ListNode{
Val: carry - 10,
}
carry = 1
}
current.Next = &node
current = &node
}
return resultList.Next
}
執(zhí)行結(jié)果
image.png
3被啼、方法三利用遞歸方法對(duì)兩個(gè)array進(jìn)行求和, 這里就不展開細(xì)講了
其他語言版本
C++
/// 時(shí)間復(fù)雜度: O(n)
/// 空間復(fù)雜度: O(n)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p1 = l1, *p2 = l2;
ListNode *dummyHead = new ListNode(-1);
ListNode* cur = dummyHead;
int carried = 0;
while(p1 || p2 ){
int a = p1 ? p1->val : 0;
int b = p2 ? p2->val : 0;
cur->next = new ListNode((a + b + carried) % 10);
carried = (a + b + carried) / 10;
cur = cur->next;
p1 = p1 ? p1->next : NULL;
p2 = p2 ? p2->next : NULL;
}
cur->next = carried ? new ListNode(1) : NULL;
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
執(zhí)行結(jié)果
image.png
Java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode cur = dummyHead;
int carry = 0;
while(l1 != null || l2 != null)
{
int sum = carry;
if(l1 != null)
{
sum += l1.val;
l1 = l1.next;
}
if(l2 != null)
{
sum += l2.val;
l2 = l2.next;
}
// 創(chuàng)建新節(jié)點(diǎn)
carry = sum / 10;
cur.next = new ListNode(sum % 10);
cur = cur.next;
}
if (carry > 0) {
cur.next = new ListNode(carry);
}
return dummyHead.next;
}
}
執(zhí)行結(jié)果
image.png
Python
class Solution(object):
def addTwoNumbers(self, l1, l2):
res=ListNode(0)
head=res
carry=0
while l1 or l2 or carry!=0:
sum=carry
if l1:
sum+=l1.val
l1=l1.next
if l2:
sum+=l2.val
l2=l2.next
# set value
if sum<=9:
res.val=sum
carry=0
else:
res.val=sum%10
carry=sum//10
# creat new node
if l1 or l2 or carry!=0:
res.next=ListNode(0)
res=res.next
return head
執(zhí)行結(jié)果
image.png
幾種語言運(yùn)行效果對(duì)比
image.png