forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0203_linkedlist.java
More file actions
50 lines (46 loc) · 1.48 KB
/
Copy pathP0203_linkedlist.java
File metadata and controls
50 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class P0203_linkedlist {
public ListNode removeElements(ListNode head,int val){
if(head==null){
return null;
}
ListNode dummy=new ListNode(-1);//为方便处理头节点开出一个虚拟节点和对应的指针,并让虚拟节点和head相连
dummy.next=head;
ListNode cur=dummy;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}
else{
cur=cur.next;
}
}
return dummy.next;
}
public ListNode removeElements1(ListNode head,int val){
while(head!=null&&head.val==val){
head=head.next;//处理头节点和要删除的值一致的情况。如果整个list都要删除,最后的next指向null
}
if (head == null) {//上一步是看头节点是否需要删除
return null;
}
ListNode cur=head;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}
else{
cur=cur.next;
}
}
return head;
}
}
/*
P0203_linkedlist p203=new P0203_linkedlist();
ListNode result=p203.removeElements1(new ListNode(new int[]{7,7,7,7,7}),7);
ListNode result1=p203.removeElements(new ListNode(new int[]{1,2,6,3,5,6}),6);
if (result != null) {
result.printListNode();//调用void类型的函数 无返回值,也没有参数
}
result1.printListNode();
*/