-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0203.java
More file actions
31 lines (26 loc) · 885 Bytes
/
_0203.java
File metadata and controls
31 lines (26 loc) · 885 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
package com.github.aditya;
public class _0203 {
// 1 ms, faster than 95.43%, memory 42.9 MB, less than 95.87%
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null)
return null;
ListNode currentNode = head;
while (currentNode.next != null) {
if (currentNode.next.val == val)
currentNode.next = currentNode.next.next;
else
currentNode = currentNode.next;
}
return head.val == val ? head.next : head;
}
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}