Sort by

recency

|

823 Discussions

|

  • + 0 comments

    Lots off problems with de code given in c#.

  • + 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

    simplest python solution:

    def deleteNode(llist, position):
        curr = llist
        prev = llist
        
        if position == 0:
            return llist.next
            
        for i in range(position):
            prev = curr
            curr = curr.next
            
        prev.next = curr.next
        
        return llist
    
  • + 0 comments

    My C++ deletion solution:

    SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) {
        SinglyLinkedListNode* ptr = llist;
        
        if (position == 0) {
            SinglyLinkedListNode* ret = llist->next;
            delete llist;
            return ret;
        }
        
        for (int i = 0; i < position-1; i++) {
            ptr = ptr->next;
        }
        
        SinglyLinkedListNode* remove = ptr->next;
        ptr->next = remove->next;
        delete remove;
        
        return llist;
    }
    
  • + 0 comments
    def deleteNode(llist, position):
        if position == 0:
            return llist.next
        
        cur = llist
        
        while position > 1:
            cur = cur.next
            position -= 1
        cur.next = cur.next.next
        return llist