2
2
3
3
import com .fishercoder .common .classes .ListNode ;
4
4
5
- /**203. Remove Linked List Elements
5
+ /**
6
+ * 203. Remove Linked List Elements
6
7
*
7
- Remove all elements from a linked list of integers that have value val.
8
-
9
- Example
10
- Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
11
- Return: 1 --> 2 --> 3 --> 4 --> 5*/
8
+ * Remove all elements from a linked list of integers that have value val.
9
+ *
10
+ * Example
11
+ * Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
12
+ * Return: 1 --> 2 --> 3 --> 4 --> 5
13
+ */
12
14
public class _203 {
13
- /**
14
- * This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY.
15
- * All the three nodes: dummy, curr and prev are indispensable.
16
- * <p>
17
- * 1. Eventually, we should return dummy.next as the final result.
18
- * 2. we assign head to curr, dummy to prev
19
- * 3. and then we use prev and curr to traverse through the list and do the work
20
- * 4. each time, we only move one node forward, so we don't need another while loop inside the while loop
21
- * 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node
22
- * else, we'll keep moving prev forward, so, just do prev = prev.next
23
- * but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside.
24
- */
25
- public ListNode removeElements (ListNode head , int val ) {
26
- ListNode dummy = new ListNode (-1 );
27
- dummy .next = head ;
28
- ListNode curr = head ;
29
- ListNode prev = dummy ;
30
- while (curr != null ) {
31
- if (curr .val == val ) {
32
- prev .next = curr .next ;
33
- } else {
34
- prev = prev .next ;
35
- }
36
- curr = curr .next ;
37
- }
38
- return dummy .next ;
39
- }
15
+ public static class Solution1 {
16
+ /**
17
+ * This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY.
18
+ * All the three nodes: dummy, curr and prev are indispensable.
19
+ *
20
+ * 1. Eventually, we should return dummy.next as the final result.
21
+ * 2. we assign head to curr, dummy to prev
22
+ * 3. and then we use prev and curr to traverse through the list and do the work
23
+ * 4. each time, we only move one node forward, so we don't need another while loop inside the while loop
24
+ * 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node
25
+ * else, we'll keep moving prev forward, so, just do prev = prev.next
26
+ * but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside.
27
+ */
28
+ public ListNode removeElements (ListNode head , int val ) {
29
+ ListNode dummy = new ListNode (-1 );
30
+ dummy .next = head ;
31
+ ListNode curr = head ;
32
+ ListNode prev = dummy ;
33
+ while (curr != null ) {
34
+ if (curr .val == val ) {
35
+ prev .next = curr .next ;
36
+ } else {
37
+ prev = prev .next ;
38
+ }
39
+ curr = curr .next ;
40
+ }
41
+ return dummy .next ;
42
+ }
43
+ }
40
44
}
0 commit comments