当前位置:网站首页>[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
边栏推荐
- 危机即机遇,远程办公效率为何会提升?
- Alibaba cloud responded to the disclosure of user registration information
- South Korea may ban apple and Google from offering commission to developers, the first in the world
- Addition, deletion, modification and query of MySQL advanced table
- GSI-ECM工程建设管理数字化平台
- IOT design and development
- Is rust more suitable for less experienced programmers?
- MySQL数据库常识之储存引擎
- The more you use the computer, the slower it will be? Recovery method of file accidental deletion
- MySQL进阶之表的增删改查
猜你喜欢
Write table of MySQL Foundation (create table)
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Pipes and xargs
Opencv application -- jigsaw puzzle
[leetcode refers to the substructure of offer 26. Tree (medium)]
Linux中,MySQL的常用命令
What if Jenkins forgot his password
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
随机推荐
Minecraft 1.12.2 module development (43) custom shield
UKFslam
Deep analysis of C language function
Detectron2 usage model
Problem brushing plan -- dynamic programming (IV)
[leetcode refers to offer 47. Maximum value of gift (medium)]
Tencent cloud has two sides in an hour, which is almost as terrible as one side..
Is rust more suitable for less experienced programmers?
GSI-ECM工程建设管理数字化平台
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
Question brushing plan - depth first search DFS (I)
IOT design and development
C# 知识
Detailed explanation of basic assembly instructions of x86 architecture
亚马逊和Epic将入驻,微软应用商城向第三方开放
Leetcode-279-complete square number
Pikachuxss how to get cookie shooting range, always fail to return to the home page
Addition, deletion, modification and query of advanced MySQL data (DML)
100天拿下11K,转岗测试的超全学习指南
使用mbean 自动执行heap dump