当前位置:网站首页>链表(一)——移除链表元素
链表(一)——移除链表元素
2022-06-28 06:04:00 【木棣%】
给你一个链表的头节点
head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。示例 :
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
提示:
- 列表中的节点数目在范围 [0, 104] 内
- 1 <= Node.val <= 50
- 0 <= val <= 50
思路
迭代(非虚拟节点)
- 找到该结点的前一个结点,进行删除操作
- 移除头结点和移除其他节点的操作不同
- 将头结点向后移动一位就可以移除该结点
- 头节点和其它节点分开

public ListNode removeElements(ListNode head, int val) {
//删除值相同的头结点后,可能新的头结点也值相等,用循环解决
while (head != null && head.val == val) {
head = head.next;
}
// 已经为null,提前退出
if (head == null) {
return head;
}
// 已确定当前head.val != val
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
迭代(虚拟节点)
- 添加一个虚拟头结点,删除头结点就不用另做考虑

public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// 因为删除可能涉及到头节点,所以设置dummy节点,统一操作
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
递归
head为空,此时直接返回head- 当
head不为空时,递归地进行删除操作 - 判断
head的节点值是否等于val并决定是否要删除head
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}
边栏推荐
- 脚本语言和编程语言
- SQL and list de duplication
- YYGH-BUG-03
- What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
- Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
- death_ satan/hyperf-validate
- MySQL common functions
- Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
- sql及list去重操作
- The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
猜你喜欢

CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题

At first glance, I can see several methods used by motionlayout

Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes

Use of JDBC

lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性

Yygh-7-user management

Sklearn Feature Engineering (summary)

Yygh-8-appointment registration

Capacity scheduling absolute value configuration queue usage and pit avoidance

socke. IO long connection enables push, version control, and real-time active user statistics
随机推荐
借助nz-pagination中的let-total解析ng-template
AutoCAD C# 多段线自相交检测
AutoCAD C# 多段线小锐角检测
RL practice (0) - and the platform xinchou winter season [rule based policy]
CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题
Working principle of es9023 audio decoding chip
ThreadLocal
【无标题】
death_satan/hyperf-validate
Idea automatically adds comments when creating classes
Apple MDM bypass jailfree bypass MDM configuration lock free
ThreadLocal
Sharing tips for efficient scripting
Oracle fundamentals summary
EasyUI reset multi condition query
Where is the era bonus for developers?
Small ball playing
YYGH-BUG-03
JQ picture amplifier
Filecoin hacker song developer competition