-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode 203
37 lines (37 loc) · 998 Bytes
/
leetcode 203
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
public class Solution {
public static void main(String[] args) {
ListNode head = new ListNode();
head.val=7;
head.next = null;
for (int i = 0; i < 7; i++) {
ListNode node = new ListNode(7,null);
node.next = head.next;
head.next = node;
}
ListNode p = removeElements(head,7);
while(p!=null){
System.out.print(p.val+" ");
p = p.next;
}
System.out.println();
}
public static ListNode removeElements(ListNode head, int val) {
ListNode fast;
ListNode slow;
if(head==null){
return head;
}
ListNode headNode = new ListNode();
headNode.next = head;
ListNode p = headNode;
while(p.next!=null){
if(p.next.val==val){
p.next = p.next.next;
}
else{
p = p.next;
}
}
return headNode.next;
}
}