LeetCode——Delete Node in a Linked List
Description:
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.
删除单向链表的一个节点,只给出了要删除的节点。
思路:从要删除的节点的下一个节点开始,逐一覆盖前面的节点的值,注意要删除最后一个多余的节点(Java中指向空即可,等待垃圾回收)。
代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
ListNode iNode = node;
while(iNode.next != null) {
//覆盖当前节点的值
iNode.val = iNode.next.val;
//删除最后一个多余的节点
if(iNode.next.next == null) {
iNode.next = null;
break;
}
iNode = iNode.next;
}
}
}
LeetCode——Delete Node in a Linked List的更多相关文章
- [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 ...
- 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 ...
- LeetCode Delete Node in a Linked List (删除链表中的元素)
题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...
- [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 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- [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_237. Delete Node in a Linked List
237. Delete Node in a Linked List Easy Write a function to delete a node (except the tail) in a sing ...
- 【4_237】Delete Node in a Linked List
Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...
随机推荐
- PHP——语句和时间函数
语句 1.分支语句 (1)if例子:$a=9;$b=5;if($a>$b){ echo $a."比".$b."大"; }else{ echo $a.&qu ...
- 网卡phy9161A
硬件1. 网口网口使用4根信号线:两根发送,两根接收.一对信号线中一根承载0——+2.5V信号电压,而另一根负载的电压是0——-2.5V,因此可产生一个5Vpp的信号差.RJ45中有用的就是4根信号线 ...
- Zookeeper使用场景
分布式系统的运行是很复杂的,因为涉及到了网络通信还有节点失效等不可控的情况.下面介绍在最传统的master-workers模型,主要可以会遇到什么问题,传统方法是怎么解决以及怎么用zookeeper解 ...
- Java GUI画圆。
package ydj; import java.awt.*; import javax.swing.*; public class huayuan extends JFrame { public h ...
- javascript 鼠标方式去显示
document.write("<style type='text/css'>"); document.write(".yyfloat_p{width:209 ...
- mac安装IDEA
Mac上安装Java7 首先我们需要去oracle下载最新的jdk,笔者拿到的最新的版本是1.7.0_45-b18,这里没有什么好说的,直接下载安装即可,安装完毕后需要在.bash_profile或者 ...
- altium designer应用技巧---cyclone IV代芯片底部焊盘问题
首先对于 altera 公司的FPGA芯片来讲,在cyclone III代以上,芯片的底部增加了一 个焊盘,很多工程师往往以为是散热用,其实不然,底部焊盘需要接地(altera手册上面 明确规定,Th ...
- 关于在Android或Java中精度缺失的解决方法
left,right是两个String类型的字符串,myres是一个double类型的变量. 如果我们用下面的语句把left,right先转换为double后直接加法的话,如果作3.3乘3之类的运算( ...
- ubuntu配置apache和cgi
ubuntu配置apache和cgi . 更新源并进行安装,否则后面的下载可能会不成功. sudo apt-get update sudo apt-get upgrade . 安装apache2服务 ...
- Supervision 行为模式
官方链接:http://erlang.org/doc/man/supervisor.html http://erlang.org/doc/design_principles/sup_princ.htm ...