Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0992ba4

Browse files
authoredJul 21, 2020
Create Remove Linked List Elements
1 parent c520c4f commit 0992ba4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎Remove Linked List Elements

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode removeElements(ListNode head, int val) {
13+
ListNode res = new ListNode(0);
14+
res.next = head;
15+
ListNode cur = res;
16+
while (cur != null && cur.next != null) {
17+
if (cur.next.val == val)
18+
cur.next = cur.next.next;
19+
else
20+
cur = cur.next;
21+
}
22+
return res.next;
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.