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.
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.
题目标签:Linked List
题目给了我们一个点,让我们在链表中删除这个点。
一般来说,要删除一个点,首先想到的是 把这个点的前面一个点 直接 链接到 这个点的 后面一个点 就可以了。
但是这一题只给了我们要删除的这个点(不会是最后一个点),所以回不到上一个点。
只要改变一下想法,用下一个点的val 复制到这一个点就可以了,然后把这一个点 链接到 下下个点就可以了。
Java Solution:
Runtime beats 9.42%
完成日期:06/12/2017
关键词:singly-linked list
关键点:把下一个点的val 复制到这一个点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public void deleteNode(ListNode node)
{
// copy next node val into this node, then link this node to next next node
ListNode nextNode = node.next;
node.val = nextNode.val;
node.next = nextNode.next; nextNode.next = null; // unlink nextNode to null }
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 237. Delete Node in a Linked List (在链表中删除一个点)的更多相关文章
- [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 ...
- 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 ...
- (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 ...
- 17.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 -David_Lin
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 删除链表结点(只给定要删除的结点) 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_Easy tag: Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
随机推荐
- android studio java.io.IOException:setDataSourse fail.
这一次是针对Android开发中的一个小问题,权限获取的问题. 在写了一个一个小Android程序的时候,有时候普需要获取本机的文件(Audio&Video),这时候如果不加权限就会出现这种情 ...
- Expectation-Maximization(EM) 算法
Expectation-Maximization 算法是统计学中用来给带隐含变量的模型做最大似然(和最大后验概率)的一种方法.EM 的应用特别广泛,经典的比如做概率密度估计用的 Gaussian Mi ...
- 十年后我不会log,还是活的很好啊
混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...
- Linux System
Linux System linux 是一个功能强大的操作系统,同时它是一个自由软件,是免费的.源代码开放的,编制它的目的是建立不受任何商品化软件版权制约的.全世界都能自由使用的UNIX兼容产品.各种 ...
- linux 下mysql无法启动 mysql.sock
在公司装的一键安装的lnmp环境,启动mysql时候发现mysql.sock不存在, 然后我进行查找 最后在 /usr/local/mysql/bin/mysql_safe 重新启动下 然后启动 ...
- 巩固JavaSE基础--IDEA完成实战项目
PS:学习完JavaSE基础后,需要有一个项目来测试自己的学习成果,并加以巩固.所以在这里,就让我们来学习下“一本糊涂账”项目吧.(此项目来源于Java自学网站) 项目完成效果图一览
- [Git]Please make sure you have the correct access rights and the repository exists
这个问题是这样,需要在已有github账号的A机器上,再创建一个github账号,新账号创建完毕,将代码通过机器A push上之后,再另一台机器B,clone 这个项目时报出了如下错误: Permis ...
- linux环境下时间的查看和修改
查看日期和时间date 查看时区date -R 查看UTC时间date -u 修改日期[root@centos ~]# date -s 20181230Sun Dec 30 00:00:00 EST ...
- python黑科技库:FuckIt.py,让你代码从此远离bug
今天给你推荐的这个库叫 “FuckIt.py”,名字一看就是很黄很暴力的那种,作者是这样介绍它的: FuckIt.py uses state-of-the-art technology to make ...
- 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb
[题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...