详细见:leetcode.com/problems/add-two-numbers/
说明:链表操作+进位
Java Solution: github
package leetcode;
/*
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain
a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
中文意思,每位存储在一个Node里面,一个数就是一个链表。
把两个链表相加一下。
*/
public class P002_Add_Two_Numbers {
public static void main(String[] args) {
ListNode t1 = new ListNode(9);
ListNode l1 = t1;
ListNode u1 = new ListNode(1);
ListNode u2 = new ListNode(8);
u1.next = u2;
ListNode l2 = u1;
ListNode ans = new Solution2().addTwoNumbers(l1, l2);
while (ans != null) {
System.out.println(ans.val);
ans = ans.next;
}
}
/*
* 31.63%
*/
static class Solution1 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ans = null, cur = null;
int carry = 0;
while (l1 != null || l2 != null) {
if (l1 != null) {
carry += l1.val;
l1 = l1.next;
}
if (l2 != null) {
carry += l2.val;
l2 = l2.next;
}
ListNode node = new ListNode(carry % 10);
carry = carry / 10;
if (ans == null) {
ans = node;
cur = node;
} else {
cur.next = node;
cur = cur.next;
}
}
if (carry != 0)
cur.next = new ListNode(carry);
return ans;
}
}
/*
* 31.63%
*/
static class Solution2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = l1, current = l1;
int assist = 0;
while (l1 != null) {
assist += l1.val;
if (l2 != null) {
assist += l2.val;
l2 = l2.next;
}
l1.val = assist % 10;
assist = assist / 10;
current = l1;
l1 = l1.next;
}
if (l2 != null) {
current.next = l2;
while (l2 != null) {
if (assist == 0)
break;
assist += l2.val;
l2.val = assist % 10;
assist = assist / 10;
current = l2;
l2 = l2.next;
}
}
if (assist != 0)
current.next = new ListNode(assist);
return head;
}
}
static class ListNode {
int val;
ListNode next;
public ListNode(int x) {
this.val = x;
}
}
}
C Solution: github
/*
url: leetcode.com/problems/add-two-numbers/
32ms 62.62%
*/
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = l1;
struct ListNode *cur = l1;
struct ListNode *tmp = NULL;
int assist = 0;
while (l1 != NULL) {
assist += l1->val;
if (l2 != NULL) {
assist += l2->val;
l2 = l2->next;
}
l1->val = assist % 10;
assist = assist / 10;
cur = l1;
l1 = l1->next;
}
if (l2 != NULL) {
cur->next = l2;
while(l2 != NULL) {
if (assist == 0) break;
assist += l2->val;
l2->val = assist % 10;
assist = assist / 10;
cur = l2;
l2 = l2->next;
}
}
if (assist != 0) {
tmp = (struct ListNode *) malloc(sizeof(struct ListNode));
tmp->val = assist;
tmp->next = NULL;
cur->next = tmp;
}
return head;
}
int main() {
}
Python Solution: github
#coding=utf-8
'''
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
'''
from leetcode.Utils import ListNode
'''
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年2月12日
@details: Solution: AC 182ms 11.93%
'''
class Solution(object):
def addTwoNumbers(self, l1, l2):
carry = 0
save = node = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1 + v2 + carry, 10)
node.next = ListNode(val)
node = node.next
return save.next
if __name__ == "__main__":
print("aaa")
Go Solution: github
package leetcode
import (
"fmt"
)
/**
* User: zxwtry
* Date: 2018/4/2
* Time: 11:48
*/
type ListNode struct {
Val int
Next *ListNode
}
func l002A_addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
ans := l1
for ; l2 != nil || l1 != nil; {
if l2 != nil {
l1.Val += l2.Val
l2 = l2.Next
}
addNode(l1)
if l1.Next == nil && l2 != nil {
l1.Next = &ListNode{0, nil}
}
l1 = l1.Next
}
return ans
}
func addNode(l *ListNode) {
if l.Val < 10 {
return
}
if l.Next == nil {
l.Next = &ListNode{l.Val / 10, nil}
l.Val %= 10
} else {
l.Next.Val += l.Val / 10
l.Val %= 10
}
}
func l002_Con(array []int) *ListNode {
arrayLen := len(array)
var now *ListNode
var pre *ListNode
pre = nil
for i := arrayLen - 1; i > -1; i -- {
now = &ListNode{array[i], pre}
pre = now
}
return now
}
func l002_Print(l *ListNode) {
arr := make([] int, 0, 10)
for ; l != nil; {
arr = append(arr, l.Val)
l = l.Next
}
fmt.Println(arr)
}
func L002_Main() {
// (2 -> 4 -> 3) + (5 -> 6 -> 4)
arr1 := [] int {2}
l1 := l002_Con(arr1)
arr2 := [] int {5, 6, 4}
l2 := l002_Con(arr2)
l3 := l002A_addTwoNumbers(l1, l2)
l002_Print(l3)
}