当前位置:网站首页>2022.06.27_ One question per day
2022.06.27_ One question per day
2022-07-05 07:11:00 【Promise い】
Title Description
Given the head pointer of one-way linked list and the value of a node to be deleted , Define a function to delete the node .
Return the head node of the deleted linked list .
Be careful : This question is different from the original one
Example 1:
Input : head = [4,5,1,9], val = 5
Output : [4,1,9]
explain : Given that the value of your list is 5 Second node of , So after calling your function , The list should be 4 -> 1 -> 9.
Example 2:
Input : head = [4,5,1,9], val = 1
Output : [4,5,9]
explain : Given that the value of your list is 1
The third node of , So after calling your function , The list should be 4 -> 5 -> 9.
explain :
Ensure that the values of nodes in the list are different from each other
If you use C or C++ Language , You don't need to free or delete Deleted node
Code
package com.nuo.Y_22_M_06;
/** * @description: TODO The finger of the sword Offer 18. Delete the node of the linked list * @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();
}
}
}
边栏推荐
- 能量守恒和打造能量缺口
- Xavier CPU & GPU 高负载功耗测试
- new和malloc的区别
- vim
- Build a microservice cluster environment locally and learn to deploy automatically
- Instruction execution time
- PHY drive commissioning - phy controller drive (II)
- ROS2——安装ROS2(三)
- 扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
- 【Node】npm、yarn、pnpm 区别
猜你喜欢

Ros2 - ros2 vs. ros1 (II)

Orin 安装CUDA环境

Instruction execution time

Ros2 - first acquaintance with ros2 (I)

Concurrent programming - how to interrupt / stop a running thread?

ROS2——ROS2对比ROS1(二)

Ros2 - common command line (IV)

ROS2——配置开发环境(五)

Ros2 - Service Service (IX)
![[software testing] 03 -- overview of software testing](/img/1e/0b6458160e34e43f021ea4797de70a.jpg)
[software testing] 03 -- overview of software testing
随机推荐
数学分析_笔记_第8章:重积分
Error: "mountvolume.setup failed for volume PVC fault handling
【软件测试】06 -- 软件测试的基本流程
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
C语言数组专题训练
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
M2DGR 多源多场景 地面机器人SLAM数据集
Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
SD_CMD_RECEIVE_SHIFT_REGISTER
Executealways of unity is replacing executeineditmode
ROS2——node节点(七)
Ros2 - workspace (V)
kata container
【软件测试】03 -- 软件测试概述
Orin 安装CUDA环境
Ros2 topic (VIII)
乐鑫面试流程
. Net core stepping on the pit practice
The problem of Chinese garbled code in the vscode output box can be solved once for life