forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (29 loc) · 938 Bytes
/
Solution.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
35
36
37
38
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
/// Time Complexity: O(n)
/// Space Complexity: O(1)
public class Solution {
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;
}
public static void main(String[] args) {
int[] arr = {1, 2, 6, 3, 4, 5, 6};
int val = 6;
ListNode head = new ListNode(arr);
System.out.println(head);
(new Solution()).removeElements(head, val);
System.out.println(head);
}
}