Related:
LeetCode 2 - Add Two Numbers
LeetCode 445 - Add Two Numbers II
LeetCode 369 - Plus One Linked List
The key point is to check the carry before return, if carry = 1 the highest bit should be 1, that means add a new node with value "1" to the result list.
Don't use variable name like: l1, use list1 or first.
https://discuss.leetcode.com/topic/799/is-this-algorithm-optimal-or-what
http://www.cnblogs.com/springfor/p/3864493.html
Code looks cleaner, but performance suffers a bit: in while loop it checks l!=null again.
usually we are not supposed to modify the arguments. So we'd better leave l1, l2 pointing to the original nodes.
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode prev = new ListNode(0); ListNode head = prev; int carry = 0; while (l1 != null || l2 != null || carry != 0) { ListNode cur = new ListNode(0); int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry; cur.val = sum % 10; carry = sum / 10; prev.next = cur; prev = cur; l1 = (l1 == null) ? l1 : l1.next; l2 = (l2 == null) ? l2 : l2.next; } return head.next; }
https://leetcode.com/discuss/19057/my-accepted-java-solution
http://www.jiuzhang.com/solutions/add-two-numbers/
https://leetcode.com/discuss/21204/12-line-java-solution
public ListNode addTwoNumbersWithCarryOver(ListNode l1,ListNode l2, int carryOver){ if (l1 == null) { return carryOver == 0 ? l2 : addTwoNumbersWithCarryOver(new ListNode(carryOver), l2,0); } if (l2 == null) { return carryOver == 0 ? l1 : addTwoNumbersWithCarryOver(l1, new ListNode(carryOver),0); } int sumVal = l1.val + l2.val + carryOver; ListNode returnVal = new ListNode(sumVal%10); returnVal.next = addTwoNumbersWithCarryOver(l1.next,l2.next, sumVal/10); return returnVal; }
http://www.programcreek.com/2012/12/add-two-numbers/
if (l1 == null && l2 == null && carry == 0) {
return null;
}
LinkedListNode result = new LinkedListNode();
int value = carry;
if (l1 != null) {
value += l1.data;
}
if (l2 != null) {
value += l2.data;
}
result.data = value % 10;
if (l1 != null || l2 != null) {
LinkedListNode more = addLists(l1 == null ? null : l1.next,
l2 == null ? null : l2.next,
value >= 10 ? 1 : 0);
result.setNext(more);
}
return result;
}
public class PartialSum {
public LinkedListNode sum = null;
public int carry = 0;
}
private static int length(LinkedListNode l) {
if (l == null) {
return 0;
} else {
return 1 + length(l.next);
}
}
private static PartialSum addListsHelper(LinkedListNode l1, LinkedListNode l2) {
if (l1 == null && l2 == null) {
PartialSum sum = new PartialSum();
return sum;
}
PartialSum sum = addListsHelper(l1.next, l2.next);
int val = sum.carry + l1.data + l2.data;
LinkedListNode full_result = insertBefore(sum.sum, val % 10);
sum.sum = full_result;
sum.carry = val / 10;
return sum;
}
private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) {
int len1 = length(l1);
int len2 = length(l2);
if (len1 < len2) {
l1 = padList(l1, len2 - len1);
} else {
l2 = padList(l2, len1 - len2);
}
PartialSum sum = addListsHelper(l1, l2);
if (sum.carry == 0) {
return sum.sum;
} else {
LinkedListNode result = insertBefore(sum.sum, sum.carry);
return result;
}
}
private static LinkedListNode padList(LinkedListNode l, int padding) {
LinkedListNode head = l;
for (int i = 0; i < padding; i++) {
head = insertBefore(head, 0);
}
return head;
}
private static LinkedListNode insertBefore(LinkedListNode list, int data) {
LinkedListNode node = new LinkedListNode(data);
if (list != null) {
node.next = list;
}
return node;
}
Read full article from Yu's Coding Garden : leetcode Question 5: Add Two Numbers
LeetCode 2 - Add Two Numbers
LeetCode 445 - Add Two Numbers II
LeetCode 369 - Plus One Linked List
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
Add each element of two list with the help of a variable "carry". When both of the lists meet their end, return result.Output: 7 -> 0 -> 8
The key point is to check the carry before return, if carry = 1 the highest bit should be 1, that means add a new node with value "1" to the result list.
Don't use variable name like: l1, use list1 or first.
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int
carry=0;
ListNode* res=
new
ListNode(0);
ListNode* head = res;
while
(l1 && l2){
res->next=
new
ListNode((l1->val+l2->val+carry)%10);
carry = (l1->val+l2->val+carry)/10;
l1=l1->next;
l2=l2->next;
res=res->next;
}
// Use one variable: this can make code cleaner:
ListNode l = l1!=null?l1:l2;
while
(l1){
res->next=
new
ListNode((l1->val+carry)%10);
carry = (l1->val+carry)/10;
l1=l1->next;
res=res->next;
}
while
(l2){
res->next=
new
ListNode((l2->val+carry)%10);
carry = (l2->val+carry)/10;
l2=l2->next;
res=res->next;
}
if
(carry>0){
res->next =
new
ListNode(carry);
}
return
head->next;
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode sentinel = new ListNode(0);
ListNode d = sentinel;
int sum = 0;
while (c1 != null || c2 != null) {
sum /= 10;
if (c1 != null) {
sum += c1.val;
c1 = c1.next;
}
if (c2 != null) {
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum % 10);
d = d.next;
}
if (sum / 10 == 1)
d.next = new ListNode(1);
return sentinel.next;
}
https://leetcode.com/problems/add-two-numbers/discuss/1059/my-accepted-java-solution public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode prev = new ListNode(0);
ListNode head = prev;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
ListNode cur = new ListNode(0);
int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
cur.val = sum % 10;
carry = sum / 10;
prev.next = cur;
prev = cur;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next;
}
return head.next;
}
http://gongxuns.blogspot.com/2012/12/leetcodeadd-two-numbers.htmlhttp://www.cnblogs.com/springfor/p/3864493.html
Code looks cleaner, but performance suffers a bit: in while loop it checks l!=null again.
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry =0; ListNode res = new ListNode(0); //use sentiment node ListNode cur1 = l1, cur2 = l2, head=res; while(cur1!=null || cur2!=null){ if(cur1!=null){ carry+=cur1.val; cur1=cur1.next; } if(cur2!=null){ carry+=cur2.val; cur2=cur2.next; } head.next=new ListNode(carry%10); head=head.next; carry/=10; } if(carry==1) head.next=new ListNode(1);//\\ return res.next; }
usually we are not supposed to modify the arguments. So we'd better leave l1, l2 pointing to the original nodes.
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode prev = new ListNode(0); ListNode head = prev; int carry = 0; while (l1 != null || l2 != null || carry != 0) { ListNode cur = new ListNode(0); int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry; cur.val = sum % 10; carry = sum / 10; prev.next = cur; prev = cur; l1 = (l1 == null) ? l1 : l1.next; l2 = (l2 == null) ? l2 : l2.next; } return head.next; }
https://leetcode.com/discuss/19057/my-accepted-java-solution
Two things to make the code simple:
- Whenever one of the two ListNode is null, replace it with 0.
- Keep the while loop going when at least one of the three conditions is met.
http://www.jiuzhang.com/solutions/add-two-numbers/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 == null && l2 == null) { return null; } ListNode head = new ListNode(0); ListNode point = head; int carry = 0; while(l1 != null && l2!=null){ int sum = carry + l1.val + l2.val; point.next = new ListNode(sum % 10); carry = sum / 10; l1 = l1.next; l2 = l2.next; point = point.next; } while(l1 != null) { int sum = carry + l1.val; point.next = new ListNode(sum % 10); carry = sum /10; l1 = l1.next; point = point.next; } while(l2 != null) { int sum = carry + l2.val; point.next = new ListNode(sum % 10); carry = sum /10; l2 = l2.next; point = point.next; } if (carry != 0) { //\\ point.next = new ListNode(carry); } return head.next; }http://bangbingsyb.blogspot.com/2014/11/leetcode-add-two-numbers.html
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *dummy = new ListNode(0), *p = dummy; int carry = 0; while(l1 || l2 || carry) { if(l1) { carry+=l1->val; l1 = l1->next; } if(l2) { carry+=l2->val; l2 = l2->next; } p->next = new ListNode(carry%10); carry /= 10; p = p->next; } return dummy->next; }X. Recusrive Version
https://leetcode.com/discuss/21204/12-line-java-solution
public ListNode addTwoNumbersWithCarryOver(ListNode l1,ListNode l2, int carryOver){ if (l1 == null) { return carryOver == 0 ? l2 : addTwoNumbersWithCarryOver(new ListNode(carryOver), l2,0); } if (l2 == null) { return carryOver == 0 ? l1 : addTwoNumbersWithCarryOver(l1, new ListNode(carryOver),0); } int sumVal = l1.val + l2.val + carryOver; ListNode returnVal = new ListNode(sumVal%10); returnVal.next = addTwoNumbersWithCarryOver(l1.next,l2.next, sumVal/10); return returnVal; }
http://www.programcreek.com/2012/12/add-two-numbers/
Sum Lists: You have two numbers represented by a linked list, where each node contains a single
digit. The digits are stored in reverse order, such that the 1 's digit is at the head of the list. Write a
function that adds the two numbers and returns the sum as a linked list.
private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2, int carry) {if (l1 == null && l2 == null && carry == 0) {
return null;
}
LinkedListNode result = new LinkedListNode();
int value = carry;
if (l1 != null) {
value += l1.data;
}
if (l2 != null) {
value += l2.data;
}
result.data = value % 10;
if (l1 != null || l2 != null) {
LinkedListNode more = addLists(l1 == null ? null : l1.next,
l2 == null ? null : l2.next,
value >= 10 ? 1 : 0);
result.setNext(more);
}
return result;
}
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
results are added to the head (i.e., passed backward). The recursive call must return the result, as before, as well as the carry
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_05_Sum_Lists/QuestionB.javapublic class PartialSum {
public LinkedListNode sum = null;
public int carry = 0;
}
private static int length(LinkedListNode l) {
if (l == null) {
return 0;
} else {
return 1 + length(l.next);
}
}
private static PartialSum addListsHelper(LinkedListNode l1, LinkedListNode l2) {
if (l1 == null && l2 == null) {
PartialSum sum = new PartialSum();
return sum;
}
PartialSum sum = addListsHelper(l1.next, l2.next);
int val = sum.carry + l1.data + l2.data;
LinkedListNode full_result = insertBefore(sum.sum, val % 10);
sum.sum = full_result;
sum.carry = val / 10;
return sum;
}
private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) {
int len1 = length(l1);
int len2 = length(l2);
if (len1 < len2) {
l1 = padList(l1, len2 - len1);
} else {
l2 = padList(l2, len1 - len2);
}
PartialSum sum = addListsHelper(l1, l2);
if (sum.carry == 0) {
return sum.sum;
} else {
LinkedListNode result = insertBefore(sum.sum, sum.carry);
return result;
}
}
private static LinkedListNode padList(LinkedListNode l, int padding) {
LinkedListNode head = l;
for (int i = 0; i < padding; i++) {
head = insertBefore(head, 0);
}
return head;
}
private static LinkedListNode insertBefore(LinkedListNode list, int data) {
LinkedListNode node = new LinkedListNode(data);
if (list != null) {
node.next = list;
}
return node;
}
Read full article from Yu's Coding Garden : leetcode Question 5: Add Two Numbers