[LeetCode] 237. Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Given linked list -- head = [4,5,1,9], which looks like following:
4 -> 5 -> 1 -> 9
Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list
should become 4 -> 1 -> 9 after calling your function.
Example 2:
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list
should become 4 -> 5 -> 9 after calling your function.
Note:
- The linked list will have at least two elements.
- All of the nodes' values will be unique.
- The given node will not be the tail and it will always be a valid node of the linked list.
- Do not return anything from your function.
写一个函数删除单链表中的一个节点,链表至少有2个元素,所有的节点值是唯一的,给的节点不会是尾部并且是合法的节点,不返回任何值。要求in-place。
解法:先把next节点的值赋给当前节点,在把当前节点的next变成当前节点的next.next。
Java:
public class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
Python:
# 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):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
Python:
class Solution:
# @param {ListNode} node
# @return {void} Do not return anything, modify node in-place instead.
def deleteNode(self, node):
if node and node.next:
node_to_delete = node.next
node.val = node_to_delete.val
node.next = node_to_delete.next
del node_to_delete
C++:
void deleteNode(ListNode* node) {
*node = *node->next;
}
C++:
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
ListNode *tmp = node->next;
node->next = tmp->next;
delete tmp;
}
};
JavaScript:
var deleteNode = function(node) {
node.val = node.next.val;
node.next = node.next.next;
};
Ruby:
def delete_node(node)
node.val = node.next.val
node.next = node.next.next
nil
end
类似题目:
[LeetCode] 203. Remove Linked List Elements 移除链表元素
All LeetCode Questions List 题目汇总
[LeetCode] 237. Delete Node in a Linked List 删除链表的节点的更多相关文章
- LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- LeetCode #237. Delete Node in a Linked List 删除链表中的节点
https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题. 题目没有给head,心想没有head我怎么才能找到要删 ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 237 Delete Node in a Linked List 删除链表的结点
编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数后,该链表应变为 ...
- [LeetCode] 237. Delete Node in a Linked List 解题思路
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- LeetCode 237. Delete Node in a Linked List (在链表中删除一个点)
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- (easy)LeetCode 237.Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- leetcode 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 t ...
随机推荐
- 小程序登录及AppSecret(小程序密钥)
在授权开发以后,需要提交小程序密钥,有小程序密钥第三方才有能力获取用户的一些信息,提供一些能力! 平台分别提供多种方式实现微信登录: 1. 调用wx.login接口,静默获取openid 适用场景:无 ...
- linux添加用户adduser出现 useradd:cannot lock /etc/passwd; try again
找一下有个叫/etc/passwd.lock的文件,找到,给它用root删掉就好了,可能是上次使用到这个文件没有正常关闭.具体操作:切换到root用户,用cd etc到etc目录下,rm .pwd.l ...
- django-改写manage类-objects
user/models.py中 class AddressManage(models.Manager): '''地址模型管理类''' def get_default_addr(self, user): ...
- 创建django项目完整实例
虚拟环境搭配 安装和配置 安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip install virtualenvwrapper # ...
- SringBoot启动报日志配置错误-logback检测异常
最近在启动项目的时候,报错,报错的原因是springBoot日志配置文件不对. 由于自己是刚接触springboot,是同事帮忙解决的,自己非常感谢! 先总结如下: 1.首先,找到logback-sp ...
- Dubbo源码分析:ProxyFactory
roxyFactory将对外开放的服务进行封装.这里使用到代理的方式.ProxyFactory接口有两个不同的实现类:JavassistProxyFactory和JdkProxyFactory.Jdk ...
- MySQL 内连接、外连接、左连接、右连接、全连接……太多了
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). 主题:内连接 ...
- B/S结构与C/S结构测试区别
B/S结构与C/S结构 B/S结构是浏览器/服务器结构,应用软件的业务逻辑完全在服务器端实现,客户端只需要通过浏览器完成浏览.查询.输入等简单操作. C/S结构是客户端/浏览器结构,客户端具有一定的数 ...
- .NET添加新项目-配置不同环境参数
添加新项目-配置不同环境参数 添加新项目后,需要对配置管理器进行设置.默认新加的项目只有debug和release 现加其他环境(dev.uat...)的配置[通过项目文件.csproj来加,拷贝其他 ...
- 11-ESP8266 SDK开发基础入门篇--软硬件定时器
https://www.cnblogs.com/yangfengwu/p/11094009.html 定时器有两种,软件定时器和硬件定时器 软件定时器就是靠里面的任务延时实现的,,这样的定时器其实延时 ...