Skip to content

Commit

Permalink
adding the leetcode and singlelinkedlist changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 committed Aug 20, 2022
1 parent 0239d78 commit 0f88e99
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Recursion/BinarySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.math.BigInteger;

public class BinarySum {

/*
* Given two binary strings a and b, return their sum as a binary string.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
*
*/
public static void main(String[] args) {
var classname = new BinarySum();
System.out.println(classname.addBinary("1010", "1011"));
}

public String addBinary(String a, String b) {
BigInteger x = new BigInteger(a, 2);
BigInteger y = new BigInteger(b, 2);
BigInteger sum = x.add(y);
return sum.toString(2);
}

}
35 changes: 35 additions & 0 deletions Recursion/FindSqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.math.BigInteger;

public class FindSqrt {


public static void main(String[] args) {


}

public int mySqrt(int x) {
BigInteger c = BigInteger.valueOf(x);
return c.sqrt().intValue();
}

public int mySqrt2(int x) {
if(x == 0){
return 0;
}
int start = 1;
int end = x;
int ans = 0;
while(start <= end){
int mid = start + (end-start)/2;
//instead of mid*mid we are giving x/mid to tackle overflow of integer range when multiplying with bigger numbers
if(mid <= x/mid){
ans = mid;
start = mid + 1;
}else{
end = mid -1;
}
}
return ans;
}
}
11 changes: 10 additions & 1 deletion SinglyLinkedList/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ else if(location>=size){

public void traverseLinkedList(Node head){
if(head==null){
System.out.println("The hea is null and there is no Linked List linked to it");
System.out.println("The head is null and there is no Linked List linked to it");
return;
}

Expand Down Expand Up @@ -139,4 +139,13 @@ else if(location==size){

}
}

public void deleteSingleLinkedList(){
if(head==null) System.out.println("the single linked list is already empty");
else {
head=null;tail=null;
System.out.println("Single linked list is deleted");
}
return;
}
}
2 changes: 2 additions & 0 deletions SinglyLinkedList/testsinglelinkedlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public static void main(String[] args) {
s.searchValueInLinkedList(10);
s.deleteNode(4);
s.traverseLinkedList(s.head);
s.deleteSingleLinkedList();
s.traverseLinkedList(s.head);
}
}

0 comments on commit 0f88e99

Please sign in to comment.