题意: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
思路:这题很有意思,一般人想到的就是直接删除这个结点就好了,但是这题其实的目的是告诉我们链表的本质在于数据的连接,所以数据才是根本,这题只要把下一个结点的数据移到这个结点,并把下一个结点删除就可以了。
代码:
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