Skip to content

Commit e4ae4ee

Browse files
refactor 203
1 parent 8229a43 commit e4ae4ee

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

src/main/java/com/fishercoder/solutions/_203.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,43 @@
22

33
import com.fishercoder.common.classes.ListNode;
44

5-
/**203. Remove Linked List Elements
5+
/**
6+
* 203. Remove Linked List Elements
67
*
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+
*/
1214
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+
}
4044
}

src/test/java/com/fishercoder/_203Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
import static org.junit.Assert.assertEquals;
1010

1111
public class _203Test {
12-
private static _203 test;
12+
private static _203.Solution1 solution1;
1313
private static ListNode head;
1414
private static ListNode expected;
1515

1616
@BeforeClass
1717
public static void setup() {
18-
test = new _203();
18+
solution1 = new _203.Solution1();
1919
}
2020

2121
@Test
2222
public void test1() {
2323
head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 6, 3, 4, 5, 6});
2424
expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5});
25-
assertEquals(expected, test.removeElements(head, 6));
25+
assertEquals(expected, solution1.removeElements(head, 6));
2626
}
2727

2828
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy