Skip to content

Commit 36f94e9

Browse files
realDuYuanChaogithub-actions
andauthored
swapping-nodes-in-a-linked-list (examplehub#97)
* remove-linked-list-elements * swapping-nodes-in-a-linked-list * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 6f73ac7 commit 36f94e9

8 files changed

Lines changed: 146 additions & 16 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import com.examplehub.leetcode.ListNode;
4+
5+
/** https://leetcode.com/problems/remove-linked-list-elements/ */
6+
public class RemoveLinkedListElements {
7+
public static ListNode solution1(ListNode head, int val) {
8+
ListNode header = new ListNode();
9+
header.next = head;
10+
ListNode cur = header;
11+
while (cur.next != null) {
12+
if (cur.next.val == val) {
13+
cur.next = cur.next.next;
14+
} else {
15+
cur = cur.next;
16+
}
17+
}
18+
return header.next;
19+
}
20+
21+
public static ListNode solution2(ListNode head, int value) {
22+
if (head == null) {
23+
return null;
24+
}
25+
head.next = solution2(head.next, value);
26+
return head.val == value ? head.next : head;
27+
}
28+
}

src/main/java/com/examplehub/leetcode/middle/AddTwoNumbers.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.leetcode.middle;
22

3+
import com.examplehub.leetcode.ListNode;
4+
35
/** https://leetcode.com/problems/add-two-numbers/ */
46
public class AddTwoNumbers {
57

@@ -61,19 +63,3 @@ public static ListNode solution2(ListNode l1, ListNode l2) {
6163
return dummyHead.next;
6264
}
6365
}
64-
65-
class ListNode {
66-
int val;
67-
ListNode next;
68-
69-
public ListNode() {}
70-
71-
public ListNode(int val) {
72-
this.val = val;
73-
}
74-
75-
ListNode(int val, ListNode next) {
76-
this.val = val;
77-
this.next = next;
78-
}
79-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.examplehub.leetcode.middle;
2+
3+
import com.examplehub.leetcode.ListNode;
4+
import com.examplehub.utils.NodeUtils;
5+
6+
/** https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ */
7+
public class SwappingNodesInALinkedList {
8+
public static ListNode solution1(ListNode head, int k) {
9+
int length = NodeUtils.length(head);
10+
11+
ListNode p1 = head;
12+
ListNode p2 = head;
13+
for (int i = 1; i <= length - k; ++i) {
14+
if (i <= k - 1) {
15+
p1 = p1.next;
16+
}
17+
p2 = p2.next;
18+
}
19+
int t = p1.val;
20+
p1.val = p2.val;
21+
p2.val = t;
22+
return head;
23+
}
24+
}

src/main/java/com/examplehub/utils/NodeUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public static ListNode makeList(int... numbers) {
3030
}
3131
return head;
3232
}
33+
34+
public static int length(ListNode head) {
35+
int count = 0;
36+
ListNode cur = head;
37+
while (cur != null) {
38+
cur = cur.next;
39+
count++;
40+
}
41+
return count;
42+
}
3343
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.leetcode.ListNode;
6+
import com.examplehub.utils.NodeUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class RemoveLinkedListElementsTest {
10+
@Test
11+
void testSolution1() {
12+
ListNode head = NodeUtils.makeList(1, 2, 6, 3, 4, 5, 6);
13+
assertEquals("1->2->3->4->5", RemoveLinkedListElements.solution1(head, 6).toString());
14+
15+
head = NodeUtils.makeList();
16+
assertSame(null, RemoveLinkedListElements.solution1(head, 1));
17+
18+
head = NodeUtils.makeList(7, 7, 7, 7);
19+
assertSame(null, RemoveLinkedListElements.solution1(head, 7));
20+
}
21+
22+
@Test
23+
void testSolution2() {
24+
ListNode head = NodeUtils.makeList(1, 2, 6, 3, 4, 5, 6);
25+
assertEquals("1->2->3->4->5", RemoveLinkedListElements.solution2(head, 6).toString());
26+
27+
head = NodeUtils.makeList();
28+
assertSame(null, RemoveLinkedListElements.solution2(head, 1));
29+
30+
head = NodeUtils.makeList(7, 7, 7, 7);
31+
assertSame(null, RemoveLinkedListElements.solution2(head, 7));
32+
}
33+
}

src/test/java/com/examplehub/leetcode/middle/AddTwoNumbersTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import com.examplehub.leetcode.ListNode;
56
import org.junit.jupiter.api.Test;
67

78
class AddTwoNumbersTest {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.examplehub.leetcode.middle;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.leetcode.ListNode;
6+
import com.examplehub.utils.NodeUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SwappingNodesInALinkedListTest {
10+
@Test
11+
void testSolution1() {
12+
ListNode head = NodeUtils.makeList(1, 2, 3, 4, 5);
13+
assertEquals("1->4->3->2->5", SwappingNodesInALinkedList.solution1(head, 2).toString());
14+
15+
head = NodeUtils.makeList(7, 9, 6, 6, 7, 8, 3, 0, 9, 5);
16+
assertEquals(
17+
"7->9->6->6->8->7->3->0->9->5", SwappingNodesInALinkedList.solution1(head, 5).toString());
18+
19+
head = NodeUtils.makeList(1);
20+
assertEquals("1", SwappingNodesInALinkedList.solution1(head, 1).toString());
21+
22+
head = NodeUtils.makeList(1, 2);
23+
assertEquals("2->1", SwappingNodesInALinkedList.solution1(head, 1).toString());
24+
25+
head = NodeUtils.makeList(1, 2, 3);
26+
assertEquals("1->2->3", SwappingNodesInALinkedList.solution1(head, 2).toString());
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.utils;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.leetcode.ListNode;
6+
import org.junit.jupiter.api.Test;
7+
8+
class NodeUtilsTest {
9+
@Test
10+
void testLength() {
11+
ListNode head = NodeUtils.makeList();
12+
assertEquals(0, NodeUtils.length(head));
13+
14+
head = NodeUtils.makeList(1, 2, 3, 4);
15+
assertEquals(4, NodeUtils.length(head));
16+
17+
head = NodeUtils.makeList(6);
18+
assertEquals(1, NodeUtils.length(head));
19+
}
20+
}

0 commit comments

Comments
 (0)