I was kind of doubting the correctness of the problem since I thought without giving a head, I cannot delete the node. Then I found that some guys are smart. They change value. :D!
+1. At first I thought this is an impossible mission. Until I saw your "change value". :D!
I find it more stupid to call this stupid without saying why it's supposedly stupid or to upvote here without saying why it's supposedly stupid. 16 people so far and nobody is capable of providing any reasoning? Then it can't really be that stupid.
Because this question encourages us to use awkward method or dummy "skills" to solve it, which deviates the general principles for linked list manipulations. When we are asked to solve questions related to linked list, it is widely accepted to just use just node operations and it is not allowed to change node's value. If the values of nodes are allowed to be changed, will it make sense to call this problem related to linked list? If values are allowed to be changed, why don't we solve it in the form of arrays?
Thanks for the explanation. I btw dislike the problem a bit as well, though just because of the wording, asking us to delete the node. Otherwise I think it's an alright problem. Yes, it requires to break a usual convention, but that's not necessarily bad. Let's think outside of the box for once.
This might even be used in the real world. Better not in a public API, at least not without warning, as the caller might have a reference to the following node, so deleting that is improper. But it could be used internally. Imagine some LRU cache implementation with a maximum capacity, for example, with a linked list holding the values in LRU order. When a value is used, it needs to be removed from its current list place. Which I think could be done like in this problem here. And since it's an internal implementation detail, it would also be safe.
"why don't we solve it in the form of arrays?"
I don't understand why you say this. Makes no sense. Removing a value from an array takes O(n) time. Removing it from a linked list takes O(1) time. The latter is far better. That's why.
The operations of linked list should base on pointer operations, however, this question misguide most people.
Solving such kind of question is nothing.
I say "why don't we solve it in the form of arrays?", because when values can be changed, how could you say linked lists are different from arrays? The values of array entries are changed very often, indeed!
I say "why don't we solve it in the form of arrays?", because when values can be changed, how could you say linked lists are different from arrays?
I already told you. They vastly differ in the speed of operations. For some things arrays are better, for some things linked lists are better. How is that not obvious? That's the whole point for having different data structures in the first place. People don't use linked lists because they find it oh so cool to handle pointers, they use them for their advantages.
def deleteNode(self, node): node.val = node.next.val node.next = node.next.next
it's leaking memory, you can see this:
@John1993 No it's not. That's Python, which handles it for you.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.