237. Delete Node in a Linked List(python)

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.
没有给出链表,只给出要删除的结点,要采用之前的方法:利用上一节点p操作来删除p.next,那么就要构造上一节点,即将下一节点的值赋给该节点,删除下一节点
Runtime: 79 ms

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteNode(self, node):
        node.val=node.next.val
        node.next=node.next.next

你可能感兴趣的:(leetcode)