当前位置:网站首页>Linked list (I) - remove linked list elements
Linked list (I) - remove linked list elements
2022-06-28 06:09:00 【Mu Di%】
Remove linked list elements
Give you a list of the head node
head And an integer val , Please delete all the contents in the linked list Node.val == val The node of , And back to New head node . Example :
Input :head = [1,2,6,3,4,5,6], val = 6
Output :[1,2,3,4,5]
Tips :
- The number of nodes in the list is in the range [0, 104] Inside
- 1 <= Node.val <= 50
- 0 <= val <= 50
Ideas
iteration ( Non virtual node )
- Find the previous node of this node , Delete operation
- Removing the head node is different from removing other nodes
- Move the head node backward one bit to remove the node
- The head node is separated from other nodes

public ListNode removeElements(ListNode head, int val) {
// After deleting the head node with the same value , Maybe the new head node has the same value , Solve with circulation
while (head != null && head.val == val) {
head = head.next;
}
// Have been to null, Exit ahead of time
if (head == null) {
return head;
}
// Determined current 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;
}
iteration ( Virtual node )
- Add a virtual head node , There is no need to consider deleting the head node

public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// Because the deletion may involve the head node , So set dummy node , Unified operation
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;
}
recursive
headIt's empty , Go straight back tohead- When
headIsn't empty , Delete recursively - Judge
headWhether the node value of is equal tovalAnd decide whether to deletehead
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;
}
}
}
边栏推荐
猜你喜欢

19 fonctions de perte d'apprentissage profond

Use of JDBC

Common basic functions of Oracle

Oracle condition, circular statement

The custom cube UI pop-up dialog supports multiple and multiple types of input boxes

Where is the era bonus for developers?

socke.io长连接实现推送、版本控制、实时活跃用户量统计

Oracle fundamentals summary

Unity packaging webgl uses IIS to solve the error

自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
随机推荐
D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
CAD secondary development +nettopologysuite+pgis reference multi version DLL
Oracle fundamentals summary
MySQL common functions
使用SSM框架,配置多个数据库连接
bash install. SH ******** error
JSP
Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
借助nz-pagination中的let-total解析ng-template
Install redis on windows and permanently change the password, and integrate redis with the SSM framework
AutoCAD C polyline self intersection detection
自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
MR-WordCount
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]]
Uni app wechat applet sharing function
Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv
Difficulty calculation of Ethereum classic
SQL and list de duplication
YYGH-6-微信登录