Skip to content

Commit 7f38c83

Browse files
solves remove linked list elements
1 parent 5bf635f commit 7f38c83

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: src/RemoveLinkedListElements.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class RemoveLinkedListElements {
2+
private static class ListNode {
3+
int val;
4+
ListNode next;
5+
6+
ListNode(int val, ListNode next) {
7+
this.val = val;
8+
this.next = next;
9+
}
10+
}
11+
12+
public static ListNode removeElements(ListNode head, int target) {
13+
ListNode result = new ListNode(-1, head), current = result;
14+
while (current != null) {
15+
if (current.next != null && current.next.val == target) {
16+
current.next = current.next.next;
17+
} else {
18+
current = current.next;
19+
}
20+
}
21+
return result.next;
22+
}
23+
}

0 commit comments

Comments
 (0)