Wednesday, January 6, 2021

LeetCode: Add Two Numbers

 Problem statement is copy pasted from Leetcode as it is:

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 contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

 

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

 

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros

My Solution: I solved 2 ways : with recursion without Recursion

package madwani.sushil.leetcode.Jan1_7_2021;

public class AddTwoNumbers {
static int carryForward = 0;
static ListNode temp = new ListNode(0);
static ListNode temp1 = temp;
public static void main(String[] args) {
ListNode inp1 = constructInput(new int[] {9,9,9,9,9,9,9});
ListNode inp2 = constructInput(new int[] {9,9,9,9});
addTwoNumbersWithRecursion(inp1, inp2); // output 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1
addTwoNumbersWithoutRecursion(inp1, inp2);
}

public static ListNode addTwoNumbersWithRecursion(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) {
return temp;
}
if (l1 == null) {
while (l2 != null) {
temp.next = new ListNode(nodeValue(l2.val, 0));
temp = temp.next;
l2 = l2.next;
}
return temp;
}
if (l2 == null) {
while (l1 != null) {
temp.next = new ListNode(nodeValue(l1.val, 0));
temp = temp.next;
l1 = l1.next;
}
return temp;
}
temp.next = new ListNode(nodeValue(l2.val, l1.val));
temp = temp.next;
addTwoNumbersWithRecursion(l1.next, l2.next);
if (carryForward != 0) {
temp.next = new ListNode(carryForward);
temp = temp.next;
carryForward = 0;
}
return temp1.next;
}

public static ListNode addTwoNumbersWithoutRecursion(ListNode l1, ListNode l2) {
while (l1 != null && l2 != null) {
temp.next = new ListNode(nodeValue(l2.val, l1.val));
temp = temp.next;
l1 = l1.next;
l2 = l2.next;
}
if (l1 != null || l2 != null) {
if (l1 == null) {
while (l2 != null) {
temp.next = new ListNode(nodeValue(l2.val, 0));
temp = temp.next;
l2 = l2.next;
}
} else if (l2 == null) {
while (l1 != null) {
temp.next = new ListNode(nodeValue(l1.val, 0));
temp = temp.next;
l1 = l1.next;
}
}
}
if (carryForward != 0) {
temp.next = new ListNode(carryForward);
}
return temp1.next;
}

public static int nodeValue(int val1, int val2) {
int val = ( val1 + val2 + carryForward ) % 10;
carryForward = ( val1 + val2 + carryForward ) / 10;
return val;
}

static ListNode constructInput(int[] ints) {
if ( ints.length == 0) {
return null;
}
ListNode inp = new ListNode(ints[0]);
ListNode inp1 = inp;
for ( int i =1; i< ints.length; i++) {
inp.next = new ListNode(ints[i]);
inp = inp.next;
}
return inp1;
}

static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) {
this.val = val; this.next = next;
}
}
}

No comments: