forked from liuyubobobo/Play-with-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution4.java
More file actions
29 lines (22 loc) · 721 Bytes
/
Copy pathSolution4.java
File metadata and controls
29 lines (22 loc) · 721 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
/// Leetcode 203. Remove Linked List Elements
/// https://leetcode.com/problems/remove-linked-list-elements/description/
class Solution4 {
public ListNode removeElements(ListNode head, int val) {
if(head == null)
return head;
ListNode res = removeElements(head.next, val);
if(head.val == val)
return res;
else{
head.next = res;
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 Solution4()).removeElements(head, 6);
System.out.println(res);
}
}