• + 0 comments

    My Java solution with linear time complexity and constant space complexity:

    public static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position) {
            if(llist == null) return null;
            
            if(position == 0){
                SinglyLinkedListNode newHead = llist.next;
                llist.next = null;
                return newHead;
            }
            
            SinglyLinkedListNode curr = llist;
            for(int i = 0; i < position; i++){
                if(i == position - 1){
                    curr.next = curr.next.next;
                } else curr = curr.next;
            }
            return llist;
        }