当前位置:网站首页>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();
}
}
}
边栏推荐
- ROS2——Service服务(九)
- Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
- Ros2 - workspace (V)
- 【软件测试】05 -- 软件测试的原则
- Orin two brushing methods
- iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
- ROS2——ROS2对比ROS1(二)
- 使用paping工具进行tcp端口连通性检测
- [software testing] 04 -- software testing and software development
- Marvell 88E1515 PHY loopback模式测试
猜你喜欢
随机推荐
Instruction execution time
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
SD_CMD_SEND_SHIFT_REGISTER
postmessage通信
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
[software testing] 05 -- principles of software testing
Mipi interface, DVP interface and CSI interface of camera
Logical structure and physical structure
About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
npm install -g/--save/--save-dev的区别
[solved] there is something wrong with the image
Qt项目中的日志库log4qt使用
2022.06.27_每日一题
Interpretation of the earliest sketches - image translation work sketchygan
new和malloc的区别
*P++, (*p) + +, * (p++) differences
SOC_SD_CMD_FSM
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
ROS2——常用命令行(四)
Binary search (half search)