当前位置:网站首页>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();
}
}
}
边栏推荐
- [solved] there is something wrong with the image
- Binary search (half search)
- GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
- Page type
- IPage can display data normally, but total is always equal to 0
- Concurrent programming - deadlock troubleshooting and handling
- new和malloc的区别
- window navicat连接阿里云服务器mysql步骤及常见问题
- Brief description of inux camera (Mipi interface)
- PHY驱动调试之 --- PHY控制器驱动(二)
猜你喜欢
An article was opened to test the real situation of outsourcing companies
postmessage通信
Ros2 - install ros2 (III)
Qt项目中的日志库log4qt使用
Ret2xx---- common CTF template proposition in PWN
Ros2 - ros2 vs. ros1 (II)
Mipi interface, DVP interface and CSI interface of camera
docker安装mysql并使用navicat连接
U-boot initialization and workflow analysis
PowerManagerService(一)— 初始化
随机推荐
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
[vscode] search using regular expressions
全局变量和静态变量的初始化
In C language, int a= 'R'
Edge calculation data sorting
C语言数组专题训练
【软件测试】06 -- 软件测试的基本流程
使用paping工具进行tcp端口连通性检测
testing framework
[nvidia] CUDA_ VISIBLE_ DEVICES
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
ROS2——配置开发环境(五)
Ros2 - configuration development environment (V)
Ros2 - common command line (IV)
Energy conservation and creating energy gap
Marvell 88E1515 PHY loopback模式测试
Lexin interview process
PHY驱动调试之 --- PHY控制器驱动(二)
Ret2xx---- common CTF template proposition in PWN
SD_CMD_SEND_SHIFT_REGISTER