Skip to content

Instantly share code, notes, and snippets.

View ugandapinik's full-sized avatar
👨‍💻
Focusing

Moris John ugandapinik

👨‍💻
Focusing
View GitHub Profile
@ugandapinik
ugandapinik / application
Created August 13, 2022 02:10
JAVA OPTIONAL CLASS
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());
@ugandapinik
ugandapinik / Application.java
Last active August 13, 2022 01:38
Custom findStudentByEmail function will fetch student information using student email address.
/* FIND SOMEONE WITH GIVEN EMAIL ADDRESS
* ==========================================================================*/
studentRepository
.findStudentByEmail("ahmed.ali@gmail.com")
.ifPresentOrElse(
System.out::println,
()-> System.out.println("Student with email ahmed.ali@gmail.com not found"));
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){
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);
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;
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
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);
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);
}
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){
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;
}