您现在的位置是:主页 > news > 网页制作教学/网络营销优化培训

网页制作教学/网络营销优化培训

admin2025/5/7 1:53:26news

简介网页制作教学,网络营销优化培训,日本人做的摇滚网站,衡水做wap网站多少钱LeetCode刷题日记删除链表中的节点Python代码删除链表中的节点 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点&#…

网页制作教学,网络营销优化培训,日本人做的摇滚网站,衡水做wap网站多少钱LeetCode刷题日记删除链表中的节点Python代码删除链表中的节点 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点&#…

LeetCode刷题日记

  • 删除链表中的节点
    • Python代码

删除链表中的节点

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 – head = [4,5,1,9],它可以表示为:

在这里插入图片描述

示例 1:

输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:

**输入: ** head = [4,5,1,9], node = 1
**输出: **[4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

说明:

  • 链表至少包含两个节点。
  • 链表中所有节点的值都是唯一的。
  • 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
  • 不要从你的函数中返回任何结果。

Python代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution(object):def deleteNode(self, node):""":type node: ListNode:rtype: void Do not return anything, modify node in-place instead."""node.val = node.next.valnode.next = node.next.next