image-20201201140204901
思路
因为只能在当前节点删除,又找不到current节点的前驱节点,所以要做的很简单:
就是把当前元素的值换成下一个元素的,然后把下下个元素赋给当前元素的下个元素。
比如 4 -> 5 -> 1 -> 9 要删除5的话,可以先把5的值改为1,然后把5的next指向9,即可达到目的
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
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
image-20201201140425531
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteNode(node *ListNode) {
node.Val = node.Next.Val
node.Next = node.Next.Next
}
image-20201201140523009
网友评论