• + 0 comments

    public static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position) { if (llist == null) return null; // If the list is empty, return null

    if (position == 0) return llist.next; // If deleting the head, return the next node
    
    SinglyLinkedListNode prev = llist;
    for (int i = 0; prev.next != null && i < position - 1; i++) {
        prev = prev.next;
    }
    
    if (prev.next != null) {
        prev.next = prev.next.next; // Skip the target node
    }
    
    return llist;
    }