/**
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
*/
class LinkedListAdditionForward {
public int carry=0;
public ListNode newHead = null;
public ListNode addTwoNumbers(ListNode h1, ListNode h2){
int len1 = getLength(h1);
int len2 = getLength(h2);
if(len1>len2){
int diff = len1-len2;
while(diff>0){
ListNode n = new ListNode(0);
n.next = h2;
h2=n;
diff--;
}
}
if(len1<len2){
int diff = len2-len1;
while(diff>0){
ListNode n = new ListNode(0);
n.next = h1;
h1=n;
diff--;
}
}
ListNode newHead = addRecursion(h1, h2);
if(carry!=0){
ListNode n = new ListNode(1);
n.next = newHead;
newHead = n;
}
return newHead;
}
public ListNode addRecursion(ListNode h1, ListNode h2){
if(h1==null && h2==null){
return null;
}
addRecursion(h1.next, h2.next);
int a = h1.val + h2.val + carry;
carry=0;
if(a>9){
carry =1;
a = a%10;
}
ListNode n = new ListNode(a);
if(newHead==null){
newHead =n;
}else{
n.next = newHead;
newHead = n;
}
return newHead;
}
public int getLength(ListNode head){
int len=0;
while(head!=null){
len++;
head = head.next;
}
return len;
}
}
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
*/
class LinkedListAdditionForward {
public int carry=0;
public ListNode newHead = null;
public ListNode addTwoNumbers(ListNode h1, ListNode h2){
int len1 = getLength(h1);
int len2 = getLength(h2);
if(len1>len2){
int diff = len1-len2;
while(diff>0){
ListNode n = new ListNode(0);
n.next = h2;
h2=n;
diff--;
}
}
if(len1<len2){
int diff = len2-len1;
while(diff>0){
ListNode n = new ListNode(0);
n.next = h1;
h1=n;
diff--;
}
}
ListNode newHead = addRecursion(h1, h2);
if(carry!=0){
ListNode n = new ListNode(1);
n.next = newHead;
newHead = n;
}
return newHead;
}
public ListNode addRecursion(ListNode h1, ListNode h2){
if(h1==null && h2==null){
return null;
}
addRecursion(h1.next, h2.next);
int a = h1.val + h2.val + carry;
carry=0;
if(a>9){
carry =1;
a = a%10;
}
ListNode n = new ListNode(a);
if(newHead==null){
newHead =n;
}else{
n.next = newHead;
newHead = n;
}
return newHead;
}
public int getLength(ListNode head){
int len=0;
while(head!=null){
len++;
head = head.next;
}
return len;
}
}
No comments:
Post a Comment