-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (30 loc) · 790 Bytes
/
Solution.java
File metadata and controls
34 lines (30 loc) · 790 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
package leetCode_203;
/**
* @author dimdark
* @date 2017-09-11
* @time 8:09 AM
*/
public class Solution {
public static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
ListNode preNode, currNode;
while (head != null && head.val == val) head = head.next; // first no-delete node
preNode = currNode = head;
while (currNode != null) {
if (currNode.val == val) {
preNode.next = currNode.next;
} else {
preNode = currNode;
}
currNode = currNode.next;
}
return head;
}
}