当前位置:网站首页>2022.06.27_每日一题
2022.06.27_每日一题
2022-07-05 07:05:00 【诺.い】
题目描述
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1
的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
说明:
题目保证链表中节点的值互不相同
若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点
代码
package com.nuo.Y_22_M_06;
/** * @description: TODO 剑指 Offer 18. 删除链表的节点 * @author nuo * @date 2022/6/27 20:12 * @version 1.0 */
public class Demo01 {
public ListNode deleteNode(ListNode head, int val) {
if (head.val == val) {
return head.next;
}
ListNode temp = head;
while (temp.next.val != val) {
temp= temp.next;
}
temp.next = temp.next.next;
return head;
}
@Test
public void test(String[] args) {
ListNode node = new ListNode(1);
node.next = new ListNode(2);
node.next.next = new ListNode(3);
node.next.next.next = new ListNode(4);
node.next.next.next.next = new ListNode(5);
node.next.next.next.next.next = new ListNode(6);
ListNode listNode = new Demo01().deleteNode(node, 5);
listNode.show();
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
public void show() {
System.out.print(this.val);
if (this.next != null) {
System.out.print("->");
this.next.show();
}
}
}
边栏推荐
- 代码中的英语全部
- 【MySQL8.0不支持表名大写-对应方案】
- Technology blog learning website
- Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
- 数学分析_笔记_第8章:重积分
- Powermanagerservice (I) - initialization
- All English in the code
- *P++, (*p) + +, * (p++) differences
- Cookie、Session、JWT、token四者间的区别与联系
- 摄像头的MIPI接口、DVP接口和CSI接口
猜你喜欢
Configuration method and configuration file of SolidWorks GB profile library
ROS2——node节点(七)
逻辑结构与物理结构
Ros2 topic (VIII)
Special training of C language array
Mutual transformation between two-dimensional array and sparse array (sparse matrix)
网易To B,柔外刚中
Pycahrm reports an error: indentation error: unindent does not match any outer indentation
window navicat连接阿里云服务器mysql步骤及常见问题
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
随机推荐
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
[nvidia] CUDA_ VISIBLE_ DEVICES
摄像头的MIPI接口、DVP接口和CSI接口
SD_CMD_RECEIVE_SHIFT_REGISTER
kata container
Logical structure and physical structure
Markdown syntax
ROS2——配置开发环境(五)
【obs】x264编码:“buffer_size“
Ros2 - node (VII)
IPage能正常显示数据,但是total一直等于0
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
小米笔试真题一
Preemption of CFS scheduling
在本地搭建一个微服务集群环境,学习自动化部署
The differences and connections among cookies, sessions, JWT, and tokens
ROS2——功能包(六)
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
Vscode configures the typera editor for MD
How to answer when you encounter a jet on CSDN?