Skip to content

Commit a075174

Browse files
realDuYuanChaogithub-actions
andauthored
delete given date of list (#129)
* delete target element of singly linkedlist * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent efd14ca commit a075174

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/main/java/com/examplehub/datastructures/linkedlist/SinglyLinkedListWithHead.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ public E delete() {
123123
return deleteTail();
124124
}
125125

126+
public E delete(E target) {
127+
Node cur = head;
128+
while (cur != null && cur.next != null && !cur.next.data.equals(target)) {
129+
cur = cur.next;
130+
}
131+
Node deletedNode = cur.next;
132+
cur.next = cur.next.next;
133+
return (E) deletedNode.data;
134+
}
135+
126136
/**
127137
* Delete a node at the head of SinglyLinkedList.
128138
*
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.examplehub.strings;
2+
3+
public class ReplaceWhiteSpace {
4+
5+
public static String solution1(String original, String str) {
6+
return original.replace(" ", str);
7+
}
8+
9+
public static String solution2(String original, String str) {
10+
char[] chars = new char[original.length() * 3];
11+
int size = 0;
12+
for (char ch : original.toCharArray()) {
13+
if (ch != ' ') {
14+
chars[size++] = ch;
15+
} else {
16+
for (char temp : str.toCharArray()) {
17+
chars[size++] = temp;
18+
}
19+
}
20+
}
21+
return String.valueOf(chars);
22+
}
23+
}

src/test/java/com/examplehub/datastructures/linkedlist/SinglyLinkedListWithHeadTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ void delete() {
8080
assertEquals("1->NULL", singlyLinkedList.toString());
8181
}
8282

83+
@Test
84+
void deleteTarget() {
85+
SinglyLinkedListWithHead<String> singlyLinkedList = new SinglyLinkedListWithHead<>();
86+
for (int i = 0; i < 5; ++i) {
87+
singlyLinkedList.insert(i + "");
88+
}
89+
assertEquals("0->1->2->3->4->NULL", singlyLinkedList.toString());
90+
assertEquals("0", singlyLinkedList.delete("0"));
91+
assertEquals("3", singlyLinkedList.delete("3"));
92+
assertEquals("4", singlyLinkedList.delete("4"));
93+
assertEquals("1->2->NULL", singlyLinkedList.toString());
94+
}
95+
8396
@Test
8497
void deleteHead() {
8598
SinglyLinkedListWithHead<String> singlyLinkedList = new SinglyLinkedListWithHead<>();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ReplaceWhiteSpaceTest {
8+
@Test
9+
void testSolution1() {
10+
assertEquals("We%20are%20happy.", ReplaceWhiteSpace.solution1("We are happy.", "%20"));
11+
}
12+
13+
@Test
14+
void testSolution2() {
15+
// FIXME
16+
// assertEquals("We%20are%20happy.", ReplaceWhiteSpace.solution2("We are happy.",
17+
// "%20"));
18+
}
19+
}

0 commit comments

Comments
 (0)