-
Notifications
You must be signed in to change notification settings - Fork 824
/
Solution2.java
34 lines (25 loc) · 842 Bytes
/
Solution2.java
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
/// Leetcode 203. Remove Linked List Elements
/// https://leetcode.com/problems/remove-linked-list-elements/description/
class Solution2 {
public ListNode removeElements(ListNode head, int val) {
while(head != null && head.val == val)
head = head.next;
if(head == null)
return head;
ListNode prev = head;
while(prev.next != null){
if(prev.next.val == val)
prev.next = prev.next.next;
else
prev = prev.next;
}
return head;
}
public static void main(String[] args) {
int[] nums = {1, 2, 6, 3, 4, 5, 6};
ListNode head = new ListNode(nums);
System.out.println(head);
ListNode res = (new Solution2()).removeElements(head, 6);
System.out.println(res);
}
}