forked from JsonChao/Awesome-Algorithm-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution203.java
More file actions
39 lines (30 loc) · 734 Bytes
/
Copy pathSolution203.java
File metadata and controls
39 lines (30 loc) · 734 Bytes
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
package LinkedList_problem;
/**
* 使用虚拟头结点
* O(n)
* O(1)
*/
public class Solution203 {
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode cur = dummyHead;
while (cur.next != null) {
if (cur.next.val == val) {
ListNode delNode = cur.next;
cur.next = delNode.next;
} else {
cur = cur.next;
}
}
return dummyHead.next;
}
}