当前位置:网站首页>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();
}
}
}
边栏推荐
- 能量守恒和打造能量缺口
- Executealways of unity is replacing executeineditmode
- The difference between new and malloc
- Huawei bracelet, how to add medicine reminder?
- Rehabilitation type force deduction brush question notes D3
- Orin 两种刷机方式
- PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
- Skywalking all
- Instruction execution time
- Special training of C language array
猜你喜欢
Use the Paping tool to detect TCP port connectivity
Qt项目中的日志库log4qt使用
[untitled]
U-Boot初始化及工作流程分析
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
Instruction execution time
LSA Type Explanation - detailed explanation of lsa-2 (type II LSA network LSA) and lsa-3 (type III LSA network Summary LSA)
. Net core stepping on the pit practice
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
Ret2xx---- common CTF template proposition in PWN
随机推荐
Architecture
*P++, (*p) + +, * (p++) differences
6-2 sequence table operation set
乐鑫面试流程
基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现
能量守恒和打造能量缺口
6-3 find the table length of the linked table
GDB code debugging
LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
What is linting
【软件测试】06 -- 软件测试的基本流程
Initialization of global and static variables
6-4 search by serial number of linked list
Skywalking全部
Orin 两种刷机方式
docker安装mysql并使用navicat连接
How to answer when you encounter a jet on CSDN?
postmessage通信
Utf8 encoding
ethtool 原理介绍和解决网卡丢包排查思路(附ethtool源码下载)