class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node.val = node.next.val node.next = node.next.next
@viakondratiuk Exactly same with yours.
@ra1den Probably there are a lot of same solutions for this problem. It doesn't give much space for your fantasy.
@viakondratiuk Makes one wonder why people still post the same solution over and over again as if it added anything to the discussion rather than diluting it, right?
@StefanPochmann I can understand you :) I think it's all about presence on internet. Just skip such solutions.
if the node you wanted to delete is the last node,your code will throw an exception,should that be below will be better?
node.val = node.next.val
node.next = node.next.next
node.val = None
but I'm not sure the last line " node.val = None " is safe or correct anyway?
@Tina_Liu You should read a task. It says: "Write a function to delete a node (except the tail) in a singly linked list, given only access to that node." So, there is no necessity to delete last node. And with your code you are not deleting last node, you just set value to None, but link on it still exists.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.