[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 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.
法一:思路是:从node 节点开始,把后面节点的值赋给前面的节点,直到到表尾;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
ListNode pos = node.next;
if (pos==null)
node=null;
else {
while (pos!=null){
node.val = pos.val;
pos = pos.next;
if (pos==null)
node.next = null;
else
node = node.next;
}
}
}
}
法二:将要删除节点后一个节点的值赋给要删除的节点,然后删除后一个节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
if(node.next==null){
node=null;
return;
}
node.val = node.next.val;
node.next = node.next.next;
}
}
[Leetcode]237. Delete Node in a Linked List -David_Lin的更多相关文章
- [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 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 ...
- 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 删除链表结点(只给定要删除的结点) 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 ...
- 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 ...
随机推荐
- [转]webpack中require和import的区别
webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require.ensure,以及webpack自己定义的require.incl ...
- Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...
- Python科学计算库
Python科学计算库 一.numpy库和matplotlib库的学习 (1)numpy库介绍:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成 ...
- VMware14 安装CentOS7 实现宿主机ping通虚拟机、虚拟机ping通宿主机、虚拟机能上网且能ping通百度
本文旨在通过通过虚拟机VMware14来安装CentOS7 系统,并配置固定IP来实现在Windows系统中使用Linux环境. 本文目录: 0.本机环境 1.VMware14 初始化 1.1.安装V ...
- linux-centos基本使用(一)
1. 基本配置 1.常用软件安装 yum install -y bash-completion vim lrzsz wget expect net-tools nc nmap tree dos2uni ...
- 在Centos中部署nginx
准备工作: nginx的安装依赖openSSL,zlib和pcre Openssl下载地址: http://www.openssl.org/ zlib下载地址: http://www.zlib.net ...
- mint17上建立lamp环境
使用apt-get方式是最简单的也是最快捷稳定的在桌面linux环境下. 分别执行如下命令: (1)安装MYSQL sudo apt-get install mysql-server ...
- [LeetCode] Loud and Rich 聒噪与富有
In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...
- jQuery-day01-介绍 和 选择器获取元素
1 ,jQuery介绍 1.1,jquery的介绍,javascript库的关系.体验jquery.把js兼容性代码封装在jquery.js中,本身就是一个javascript库. 1.2,jQuer ...
- Nginx负载均衡的5种策略(转载)
Nginx的upstream目前支持的5种方式的分配 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream backserver { s ...