List Does Not Change In Attempt To Solve "Delete Node In Doubly Linked List"
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.
The task is to complete the function
deleteNode()which should delete the node at given position and return the head of the linkedlist.
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?
whilecondition is wrong: it is false immediately, so the loop will not execute.
The value for
Nonewhen you dereference it with
prev_of_delete_node.next. So guard that operation. Same for
The function doesn't return anything, but it should return the head of the list after the deletion
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
- → How to configure JSON for graphql query?
- → Google couldn't fetch my sitemap.xml file
- → A lot of socket endpoints in python?
- → Historical price per minute between two timestamps for a cryptocurrency
- → How to get a value from a list of dictionaries - Python 3.7.1
- → What is the optimal way to parse these strings in Python?
- → Short Order on Binance futures testnet resulting in APIError (ReduceOnly Order is Rejected)
- → values subtracted while iterating through list has random miscalculations
- → Foreign key query error in case of custom uint64 field which was used as a primary key
- → Grouping all tests Python
- → Using #!python2 not working to run under Python 2
- → Storing last 3 scores and deleting older scores and calculating average?
- → Checking if input is in a list of numbers in python