当前位置:网站首页>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();
}
}
}
边栏推荐
- 解读最早的草图-图像翻译工作SketchyGAN
- Ros2 - node (VII)
- Error: "mountvolume.setup failed for volume PVC fault handling
- Powermanagerservice (I) - initialization
- Unity ugui how to match and transform coordinates between different UI panels or uis
- Cloud native related technology learning
- 二分查找(折半查找)
- Ros2 - configuration development environment (V)
- The difference between new and malloc
- IPage能正常显示数据,但是total一直等于0
猜你喜欢
随机推荐
SOC_SD_CMD_FSM
Special training of C language array
基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现
Ros2 - Service Service (IX)
ROS2——常用命令行(四)
Xavier CPU & GPU high load power consumption test
C#学习笔记
CGroup CPU group source code analysis
Logical structure and physical structure
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
IPage能正常显示数据,但是total一直等于0
The differences and connections among cookies, sessions, JWT, and tokens
Technical conference arrangement
【软件测试】04 -- 软件测试与软件开发
Technology blog learning website
Volcano 资源预留特性
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
Markdown syntax







