add two linked list as integer
2012-08-28 23:17
375 查看
Question:
You have two numbers represented by a linked list, where each node contains a single digit. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE:
input: (3 -> 1 -> 5), (5 -> 9 -> 2)
output: 9 -> 0 -> 7
Analyze:
use two stacks to store the numbers in the list, and then the last number of the lists will be on the top of the stacks, we pop the stack and add the numbers, and save the result into the linked list.
Code:
public static LinkedList addList(Node h1, Node h2) {
//save the numbers in the list to stacks
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
while(h1 != null) {
s1.push(h1);
h1 = h1.next;
}
while(h2 != null) {
s2.push(h2);
h2 = h2.next;
}
//LinkedList saves the result
LinkedList list = new LinkedList();
int sum = 0;
int carry = 0;
while(!s1.empty() && !s1.empty()) {
sum = s1.pop().data + s2.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
while(!s1.empty()) {
sum = s1.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
while(!s2.empty()) {
sum = s1.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
// don't forget the carry
if (carry != 0) list.addFirst(carry);
return list;
}http://blog.csdn.net/beiyeqingteng
You have two numbers represented by a linked list, where each node contains a single digit. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE:
input: (3 -> 1 -> 5), (5 -> 9 -> 2)
output: 9 -> 0 -> 7
Analyze:
use two stacks to store the numbers in the list, and then the last number of the lists will be on the top of the stacks, we pop the stack and add the numbers, and save the result into the linked list.
Code:
public static LinkedList addList(Node h1, Node h2) {
//save the numbers in the list to stacks
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
while(h1 != null) {
s1.push(h1);
h1 = h1.next;
}
while(h2 != null) {
s2.push(h2);
h2 = h2.next;
}
//LinkedList saves the result
LinkedList list = new LinkedList();
int sum = 0;
int carry = 0;
while(!s1.empty() && !s1.empty()) {
sum = s1.pop().data + s2.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
while(!s1.empty()) {
sum = s1.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
while(!s2.empty()) {
sum = s1.pop().data + carry;
carry = sum / 10;
sum = sum % 10;
list.addFirst(sum);
}
// don't forget the carry
if (carry != 0) list.addFirst(carry);
return list;
}http://blog.csdn.net/beiyeqingteng
相关文章推荐
- add two linked list as integer
- [CrackCode] 2.4 Add two numbers and return the sum as a linked list
- LinkedList-445-Add Two Numbers II
- The two numbers and return it as a linked list
- 【12】Adds two numbers represented by a linked list and returns the sum as a linked list
- LinkedList-2-Add Two Numbers
- (LinkedList)2. Add Two Numbers
- 2. Add Two Numbers(Linked List)
- Linked List -- Leetcode problem 2. Add Two Numbers
- C++: linked list as an ADT(abstract data types)
- 2. Add Two Numbers leetcode(list)
- Swap Two Nodes in Linked List
- 【算法世界】(九)LeetCode -- Add Two Numbers by ListNode
- [leetcode][list] Intersection of Two Linked Lists
- 2.swap-two-nodes-in-linked-list(交换链表中的两个结点)
- leetcode Merge Two Linked List
- 使用DoubleLinkedList扩展类,允许Add,Remove,Contains
- 【Leetcode】Intersection of Two Linked List
- lintcode-medium-Swap Two Nodes in Linked List
- leetcode 21. Merge Two Sorted Lists Add to List