Why doesn't it work?
def deleteNode(self, node):
node = node.next
sorry I'm a beginner
"node" is just a reference, like a pointer. node =node.next just changes node from pointing to original node to next node. But the nodelist itself doesn't change.
What happens when node is None or node.next is None? And why we do not need to "delete" the allocated memory for node in Python as we did in C++?
When node is None or node is the last node, you should consider them separately. If the node is the last one, we have no other way except scanning from the beginning to the end, so the complexity is O(n), while this is the only case, other nodes can be done by replacing values, so the average complexity is still O(1). In Python, we don't need to contact with memory directly, the underlying memory manager will handle this for us. Here you can see the detailed description.
I think one line is ok.
class Solution(object): def deleteNode(self, node): node.val, node.next = node.next.val, node.next.next
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.