Add Two Numbers


算法:链表 Linked list

题目:Add Two Numbers

url:https://leetcode.com/problems/add-two-numbers/

1
2
3
4
5
6
7
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.

Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

分析

  • 百位大数相加

Java解法

方法 定义
链表增加节点
单链表反转 不需要,只是为了练习算法
遍历链表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.test.demo;

import java.io.IOException;

class ListNode {
int val;
ListNode next;
public ListNode(int x) {this.val = x;}
}

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode retNode = null;
int carryBit = 0;
while(l1 != null || l2!= null) {
int d1 = null==l1 ? 0 : l1.val;
int d2 = null==l2 ? 0 : l2.val;
int d = d1 + d2 + carryBit;
retNode = add(retNode, new ListNode(d%10));
carryBit = d/10;
l1 = null==l1 ? l1 : l1.next;
l2 = null==l2 ? l2 : l2.next;
}
for (int i = carryBit; i>0; i = i/10) {
retNode = add(retNode, new ListNode(i%10));
}
return retNode;
// return reverse(retNode);
}
public ListNode add(ListNode headNode, ListNode node) {
if (null == headNode) {
headNode = node;
} else {
ListNode index = headNode;
while(index.next != null) {index = index.next;}
index.next = node;
}
return headNode;
}
public ListNode reverse(ListNode headNode) {
if(headNode == null) return headNode;
ListNode pre = headNode;
ListNode cur;
ListNode temp;
for(cur = headNode.next; cur!=null; ) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
headNode.next = null;
return pre;
}
}

public class MainClass {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}

String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}

public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input);

// Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
}

public static String listNodeToString(ListNode node) {
if (node == null) {
return "[]";
}

String result = "";
while (node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
}

public static void main(String[] args) throws IOException {

ListNode l1 = stringToListNode("[9]");
ListNode l2 = stringToListNode("[1,9,9,9,9,9,9,9,9,9]");

ListNode ret = new Solution().addTwoNumbers(l1, l2);
String out = listNodeToString(ret);

System.out.print(out);
}
}

Python解法