Skip to content

Commit 7fa0347

Browse files
authored
Update Remove Linked List Elements.java
1 parent 2f8fc22 commit 7fa0347

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Easy/Remove Linked List Elements.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
*/
1111
class Solution {
1212
public ListNode removeElements(ListNode head, int val) {
13-
while (head != null && head.val == val) {
14-
head = head.next;
13+
ListNode newHead = null;
14+
ListNode curr = head;
15+
while (curr != null && curr.val == val) {
16+
curr = curr.next;
1517
}
16-
ListNode newHead = head;
17-
ListNode curr = newHead;
18+
newHead = curr;
1819
while (curr != null && curr.next != null) {
1920
if (curr.next.val == val) {
2021
curr.next = curr.next.next;
21-
}
22-
else {
22+
} else {
2323
curr = curr.next;
2424
}
2525
}

0 commit comments

Comments
 (0)