当前位置:网站首页>[leetcode refers to offer 18. Delete the node of the linked list (simple)]
[leetcode refers to offer 18. Delete the node of the linked list (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
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
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head == null)
return null;
ListNode pre = head;
if(head.val == val){
return head.next;
}
while(pre.next!=null && pre!=null){
if(pre.next.val==val){
if(pre.next.next!=null)
pre.next = pre.next.next;
else{
pre.next = null;
break;
}
}
pre=pre.next;
}
return head;
}
}
Execution results :

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479283.html
边栏推荐
- Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
- Automatic heap dump using MBean
- Getting started with detectron2
- opencv应用——以图拼图
- How to play the guiding role of testing strategy
- Normalized transforms in pytorch The real calculation process of normalize
- Tensorflow1. X and 2 How does x read those parameters saved in CKPT
- PHP的Laravel与Composer部署项目时常见问题
- Opencv application -- jigsaw puzzle
- Pytorch selects the first k maximum (minimum) values and their indexes in the data
猜你喜欢

Problem brushing plan -- dynamic programming (IV)

MySQL basic collection

The more you use the computer, the slower it will be? Recovery method of file accidental deletion

Google 尝试在 Chrome 中使用 Rust

Express ③ (use express to write interface and cross domain related issues)

Some grounded words

CUDA, NVIDIA driver, cudnn download address and version correspondence

100天拿下11K,转岗测试的超全学习指南

Win 11K in 100 days, super complete learning guide for job transfer test

一文解决浏览器跨域问题
随机推荐
Pytorch selects the first k maximum (minimum) values and their indexes in the data
Thread safe sigleton (singleton mode)
thinkphp5+数据大屏展示效果
Presto on spark supports 3.1.3 records
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Problem brushing plan -- dynamic programming (III)
Selenium displays webdriverwait
【SDU Chart Team - Core】SVG属性类设计之枚举
Send email to laravel
2.整理华子面经--2
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
Another data analysis artifact: Polaris is really powerful
引入结构化并发,Swift 5.5 发布!
Normalized transforms in pytorch The real calculation process of normalize
Thinking after learning to type
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
ros功能包内自定义消息引用失败
MySQL advanced common functions
Deno 1.13.2 发布