This page contains some tricky but common small algorithm snippets.

Reverse a Linked List

fun reverseList(head: ListNode?): ListNode? {
    var node = head
    var prev: ListNode? = null
    while(node != null) {
        var temp = node.next
        node.next = prev
        prev = node 
        node = temp
    }
    return prev
}

Delete a node from Linked List if head is not known

public void deleteNode(ListNode node) {
    node.val =  node.next.val;
    node.next = node.next.next;
}

GCD of two numbers

public int gcd(int a, int b) {
    while(b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}