List does not change in attempt to solve "Delete node in Doubly Linked List"

Abhishek Sanwal

I am working on the GeeksForGeeks problem Delete node in Doubly Linked List:

Given a doubly linked list and a position. The task is to delete a node from given position in a doubly linked list.

Your Task:

The task is to complete the function deleteNode() which should delete the node at given position and return the head of the linkedlist.

My code:

def deleteNode(self,head, x):
    # Code here
    temp=head        
    count_of_nodes=0
    prev_of_delete_node=None
    next_of_delete_node=None
    while temp != head:
        count_of_nodes+=1
        if count_of_nodes==x:
            prev_of_delete_node=temp.prev
            next_of_delete_node=temp.next
            #print(y.data,z.data)
            prev_of_delete_node.next=next_of_delete_node
            next_of_delete_node.prev=prev_of_delete_node
            break
        
        temp=temp.next
    
    if x==1:
        head=next_of_delete_node

There is no effect on the doubly LinkedList after executing above code. Why is this?

trincot

Some issues:

  • The while condition is wrong: it is false immediately, so the loop will not execute.

  • The value for prev_of_delete_node could be None when you dereference it with prev_of_delete_node.next. So guard that operation. Same for next_of_delete_node.

  • The function doesn't return anything, but it should return the head of the list after the deletion

Correction:

def deleteNode(self,head, x): 
    temp=head
    count_of_nodes=0
    prev_of_delete_node=None
    next_of_delete_node=None
    while temp:  # Corrected loop condition
        count_of_nodes+=1
        if count_of_nodes==x:
            prev_of_delete_node=temp.prev
            next_of_delete_node=temp.next
            if prev_of_delete_node:  # Guard
                prev_of_delete_node.next=next_of_delete_node
            if next_of_delete_node:  # Guard
                next_of_delete_node.prev=prev_of_delete_node
            break
        
        temp=temp.next
    # Should return:
    if x==1:
        return next_of_delete_node
    return head 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

delete node in doubly linked list (Data Structures)

How to search name and delete node in doubly linked list in C?

Why does my attempt at Change Detection in Bevy never trigger?

Text input does not change slider value on first attempt

Why does vstack change the type of the elments? And how do I solve this?

What to do when I get an "attempt to read or write outside of disk 'hd0'" error and Boot Repair does not solve the problem?

How to solve 'attempt to invoke virtual methods' of ViewModel

Why does the expert change MutableList to List?

Recursive function in Python does not change list

Why does the other list change as well?

Last item of list does not change after adding

why does a list change after looping

Multiprocess does not apply list change to all processes

Python: Why does the id change after copying a list with b = list(a)

Does changing an already pushed list(on a vector) change the list that is on a vector?

Why does the original list change when changing the new list?

When does a pointer to a linked list change the actual list?

Does the value change if I change a numpy array to a list and then change the list to a numpy array?

Why does Kotlin change a list of type List<List<Int>> to List<Any> if a list is added with the "+" operator?

How can I solve error in ggplot, which does not want to change label/legend/title size?

How to change structure of list items with JS to solve inline-block's generated space between them

How to solve this : Exception encountered during context initialization - cancelling refresh attempt?

how to solve this error: attempt to set 'rownames' on an object with no dimensions

how to solve this error : attempt to make a table with >= 2^31 elements?

How to solve attempt to read property "id" on null (Laravel-8)

How to change Laravel Auth::attempt method query?

SAS : ERROR Attempt to change the value of a string in PRXCHANGE

Sagemath expression does not solve

Why does the original list change when I change the copied list (PYTHON)