This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
Optional<String> hello = Optional.ofNullable(null); | |
// 1. Optional Use: | |
// check value is present in hello | |
System.out.println(hello.isPresent()); | |
// check if hello(v) is empty | |
System.out.println(hello.isEmpty()); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FIND SOMEONE WITH GIVEN EMAIL ADDRESS | |
* ==========================================================================*/ | |
studentRepository | |
.findStudentByEmail("[email protected]") | |
.ifPresentOrElse( | |
System.out::println, | |
()-> System.out.println("Student with email [email protected] not found")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int findNthLinkedList(Node head, int n) { | |
Node runner = head; | |
Node walker = head; | |
for (int i = 0; i < n; i++){ | |
runner = runner.next; | |
} | |
if (runner == null) return head.next.data; | |
while (runner != null){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static boolean hasCycle(SinglyLinkedListNode head) { | |
SinglyLinkedListNode first = head; | |
SinglyLinkedListNode second = head; | |
do{ | |
first = first.next; | |
second = second.next; | |
second = second != null ? second.next : null; | |
}while (first != null && second != null && first != second); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Node insertData(Node llist, int data){ | |
// Write your code here | |
Node curr = llist; | |
Node node = new Node(data); | |
if (llist == null) return node; | |
else if(node.data < curr.data){ | |
// insert in the beginning | |
node.next = curr; | |
curr.prev = node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static SinglyLinkedListNode reverse(SinglyLinkedListNode llist) { | |
// Write your code here | |
SinglyLinkedListNode curr = llist; | |
SinglyLinkedListNode next = llist.next; | |
llist.next = null; // 1->null | |
while(next != null){ | |
SinglyLinkedListNode temp = next.next; // 3->4->5 | |
next.next = curr; // 2->1->null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void traverse(int[][] G, int start){ | |
int[] visited = new int[G.length]; | |
Queue<Integer> queue = new LinkedList<>(); | |
System.out.print(start + " "); | |
// visited first 1 node | |
visited[start] = 1; | |
// insert into the queue | |
queue.add(start); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[] visited = new int[8]; | |
public void traverse(int[][] G, int u){ | |
if (visited[u] == 0){ | |
System.out.print(u + " "); | |
visited[u] = 1; | |
for (int v = 1; v <G.length; v++){ | |
if (G[u][v] == 1 && visited[v] == 0){ | |
traverse(G, v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int coinChange(int[] coins, int amount){ | |
return recursionHelper(coins, amount); | |
} | |
private static int recursionHelper(int[] coins, int remaining) { | |
if (remaining == 0) return 0; | |
if (remaining < 0) return -1; | |
int minCount = Integer.MAX_VALUE; | |
for (int coin: coins){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] plusOne(int[] digits) { | |
// digits = [1, 2, 3] // output: [1, 2, 4] | |
// digits = [9, 9, 9] // output: [1, 0, 0, 0] | |
for(int idx = digits.length - 1; idx >= 0; idx--){ | |
if(digits[idx] < 9){ | |
digits[idx] += 1; | |
return digits; | |
} | |
NewerOlder