Sort by

recency

|

829 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can have the video explanation here : https://youtu.be/EjWw69vLVYo

    SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) {
        if(position == 0) return llist->next;
        SinglyLinkedListNode* curr = llist;
        while(position - 1) {
            curr = curr -> next;
            position--;
        }
        curr->next = curr->next->next;
        return llist;
    }
    
  • + 0 comments

    here's my solution in C language:

    SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) { SinglyLinkedListNode *fnnode = llist; if(llist == NULL || position <= -1){ return llist; } else if (position == 0){ llist = llist->next; free(fnnode); return llist; } else{ int counter = 0; while(fnnode != NULL && counter < position - 1){ fnnode = fnnode -> next; counter++; } if(fnnode != NULL && fnnode->next != NULL){ SinglyLinkedListNode *delf = fnnode -> next; fnnode -> next = fnnode->next->next; free(delf); } } return llist; }

  • + 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;
    }
    
  • + 0 comments
         if (position == 0){
        if (head.next == null){
            return null;
        }
        else {
            return head.next;
        }
    }
    else {
        SinglyLinkedListNode current = head;
        for (int i = 0;i < position - 1; i++){
            current = current.next;
        }
        current.next = current.next.next;
        return head;
    }
    
    }
    
  • + 1 comment

    Why it's not working in C#.? Is there any way to resolve in C#??

    • + 0 comments

      even i think so