Skip to content

Commit 159c520

Browse files
RemoveNthNodeFromEndOfList
1 parent 30397f8 commit 159c520

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.examplehub.leetcode;
2+
3+
public class ListNode {
4+
public int val;
5+
public ListNode next;
6+
7+
public ListNode() {
8+
}
9+
10+
public ListNode(int val) {
11+
this.val = val;
12+
}
13+
14+
ListNode(int val, ListNode next) {
15+
this.val = val;
16+
this.next = next;
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.examplehub.leetcode.middle;
2+
3+
import com.examplehub.leetcode.ListNode;
4+
5+
/**
6+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/
7+
*/
8+
public class RemoveNthNodeFromEndOfList {
9+
public static ListNode solution1(ListNode head, int n) {
10+
int length = 0;
11+
12+
/* calculate length of list */
13+
ListNode current = head;
14+
while (current != null) {
15+
length++;
16+
current = current.next;
17+
}
18+
19+
if (length <= 1) {
20+
head = null;
21+
} else if (length == n) {
22+
head = head.next;
23+
} else {
24+
current = head;
25+
for (int i = 1; i <= length - n - 1; i++) {
26+
current = current.next;
27+
}
28+
current.next = current.next.next;
29+
}
30+
return head;
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.examplehub.utils;
2+
3+
import com.examplehub.leetcode.ListNode;
4+
5+
import java.util.StringJoiner;
6+
7+
public class NodeUtils {
8+
public static String toString(ListNode head) {
9+
ListNode current = head;
10+
StringJoiner joiner = new StringJoiner("->");
11+
while (current != null) {
12+
joiner.add(current.val + "");
13+
current = current.next;
14+
}
15+
joiner.add("NULL");
16+
return joiner.toString();
17+
}
18+
19+
public static ListNode makeList(int... numbers) {
20+
ListNode head = null;
21+
ListNode tail = null;
22+
for (int number : numbers) {
23+
ListNode newNode = new ListNode(number);
24+
newNode.next = null;
25+
if (head == null) {
26+
tail = head = newNode;
27+
} else {
28+
tail.next = newNode;
29+
tail = tail.next;
30+
}
31+
}
32+
return head;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.examplehub.leetcode.middle;
2+
3+
import com.examplehub.leetcode.ListNode;
4+
import com.examplehub.utils.NodeUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class RemoveNthNodeFromEndOfListTest {
10+
11+
@Test
12+
void testSolution1() {
13+
ListNode head = NodeUtils.makeList(1, 2, 3, 4, 5);
14+
assertEquals("1->2->3->4->5->NULL", NodeUtils.toString(head));
15+
16+
head = RemoveNthNodeFromEndOfList.solution1(head, 2);
17+
assertEquals("1->2->3->5->NULL", NodeUtils.toString(head));
18+
19+
head = NodeUtils.makeList(1, 2);
20+
assertEquals("1->2->NULL", NodeUtils.toString(head));
21+
head = RemoveNthNodeFromEndOfList.solution1(head, 1);
22+
assertEquals("1->NULL", NodeUtils.toString(head));
23+
24+
head = NodeUtils.makeList(1);
25+
assertEquals("1->NULL", NodeUtils.toString(head));
26+
head = RemoveNthNodeFromEndOfList.solution1(head, 1);
27+
assertEquals("NULL", NodeUtils.toString(head));
28+
29+
head = NodeUtils.makeList(1, 2);
30+
assertEquals("1->2->NULL", NodeUtils.toString(head));
31+
head = RemoveNthNodeFromEndOfList.solution1(head, 2);
32+
assertEquals("2->NULL", NodeUtils.toString(head));
33+
}
34+
}

0 commit comments

Comments
 (0)