Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

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;
}
}
}

LeetCode: Remove Duplicates from Sorted List I

The Problem statement is copied from Leetcode as it is:

Given the
 head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

 

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

 

Constraints:

  • The number of nodes in the list is in the range [0, 300].
  • -100 <= Node.val <= 100
  • The list is guaranteed to be sorted in ascending order.

My Solution for the problem:

package madwani.sushil.leetcode.Jan1_7_2021;

public class Remove_Duplicates_From_Sorted_List_I {
public static void main(String[] args) {
ListNode inp1 = constructInput(new int[] {1, 1, 1 , 2, 2, 2, 3, 4, 5 , 6, 6});
ListNode inp2 = constructInput(new int[] {-1 , -2, -2, 2, 3, 4, 5 , 6, 6});
ListNode inp3 = constructInput(new int[0]);
ListNode inp4 = constructInput(new int[] {1, 2, 3, 4, 5, 6});
deleteDuplicates(inp1); // output : [1,2,3,4,5,6]
deleteDuplicates(inp2); // output : [-1,-2,2,3,4,5,6]
deleteDuplicates(inp3); // output : null
deleteDuplicates(inp4); // output : [1, 2, 3, 4, 5, 6]
deleteDuplicates(null); // output: null
}

public static ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode temp = new ListNode(
-2000, head);
ListNode temp1 = temp;
while (temp1 !=null && temp1.next !=null) {
if (temp1.val == temp1.next.val) {
temp1.
next = temp1.next.next;
} else {
temp1 = temp1.
next;
}
}
return temp.next;
}

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;
}
}
}

LeetCode: Remove Duplicates from Sorted List II

 The question is copied from LeetCode:

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

 

Example 1:

Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]

Example 2:

Input: head = [1,1,1,2,3]
Output: [2,3]

 

Constraints:

  • The number of nodes in the list is in the range [0, 300].
  • -100 <= Node.val <= 100
  • The list is guaranteed to be sorted in ascending order.

My Solution:

package madwani.sushil.leetcode.Jan1_7_2021;

public class Remove_Duplicates_From_Sorted_List_II {
static int val = -1000;
public static void main(String[] args) {
ListNode inp1 = constructInput(new int[] {1, 1, 1 , 2, 2, 2, 3, 4, 5 , 6, 6});
deleteDuplicates(inp1);
}

public static ListNode deleteDuplicates(ListNode head) {
ListNode temp = new ListNode(0);
temp.next = head;
ListNode temp1 = temp;
while (temp1.next !=null && temp1.next.next !=null) {
if (temp1.next.val == temp1.next.next.val) {
val = temp1.next.val;
while(temp1.next !=null && temp1.next.val == val) {
temp1.next = temp1.next.next;
}
} else {
temp1 = temp1.next;
}
}
return temp.next;
}

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; }
}
}