-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ203_RemoveLinkedListElements.java
More file actions
36 lines (34 loc) · 1.03 KB
/
Copy pathQ203_RemoveLinkedListElements.java
File metadata and controls
36 lines (34 loc) · 1.03 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
package leetcode;
public class Q203_RemoveLinkedListElements {
static class ListNode{
int val;
ListNode next;
ListNode(int x){ val = x; }
}
public static ListNode removeElements(ListNode head, int val) {
if (head == null) return null; //空
while((head!=null)&&(head.val == val)) head = head.next; //前导val
ListNode t = head; //遍历指针
while((t!=null)&&(t.next!=null)){ //枚举
if (t.next.val == val)
t.next = t.next.next; //越过val
else
t = t.next;
}
return head;
}
public static void main(String[] args) {
ListNode ans = new ListNode(1);
ans.next = new ListNode(2);
ans.next.next = new ListNode(6);
ans.next.next.next = new ListNode(3);
ans.next.next.next.next = new ListNode(4);
ans.next.next.next.next.next = new ListNode(5);
ans.next.next.next.next.next.next = new ListNode(6);
ans = removeElements(ans, 1);
while(ans != null){
System.out.print(ans.val);
ans = ans.next;
}
}
}