Skip to content

Commit c4c49d4

Browse files
committed
Add RemoveNthNodeFromLinkedListEnd task solution
1 parent 481155a commit c4c49d4

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package by.andd3dfx.collections;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.NoArgsConstructor;
5+
6+
/**
7+
* <pre>
8+
* <a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">Task description</a>
9+
*
10+
* Given the head of a linked list, remove the nth node from the end of the list and return its head.
11+
*
12+
* Example 1:
13+
* Input: head = [1,2,3,4,5], n = 2
14+
* Output: [1,2,3,5]
15+
*
16+
* Example 2:
17+
* Input: head = [1], n = 1
18+
* Output: []
19+
*
20+
* Example 3:
21+
* Input: head = [1,2], n = 1
22+
* Output: [1]
23+
*
24+
* Constraints:
25+
* The number of nodes in the list is sz.
26+
* 1 <= sz <= 30
27+
* 0 <= Node.val <= 100
28+
* 1 <= n <= sz
29+
* </pre>
30+
*
31+
* @see <a href="https://youtu.be/Ant-eAAwyx8">Video solution</a>
32+
*/
33+
public class RemoveNthNodeFromLinkedListEnd {
34+
35+
@NoArgsConstructor
36+
@AllArgsConstructor
37+
public static class ListNode {
38+
39+
int val;
40+
ListNode next;
41+
42+
ListNode(int val) {
43+
this.val = val;
44+
}
45+
}
46+
47+
public static ListNode removeNthFromEnd(ListNode head, int n) {
48+
var newHead = new ListNode();
49+
newHead.next = head;
50+
51+
var first = newHead;
52+
var second = newHead;
53+
54+
for (var i = 0; i < n; i++) {
55+
second = second.next;
56+
}
57+
58+
while (second.next != null) {
59+
first = first.next;
60+
second = second.next;
61+
}
62+
63+
first.next = first.next.next;
64+
65+
return newHead.next;
66+
}
67+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package by.andd3dfx.collections;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import by.andd3dfx.collections.RemoveNthNodeFromLinkedListEnd.ListNode;
6+
import org.junit.Test;
7+
8+
public class RemoveNthNodeFromLinkedListEndTest {
9+
10+
@Test
11+
public void removeNthFromEnd1() {
12+
var head = new ListNode(1,
13+
new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
14+
15+
var result = RemoveNthNodeFromLinkedListEnd.removeNthFromEnd(head, 2);
16+
17+
assertThat(result.val).isEqualTo(1);
18+
assertThat(result.next.val).isEqualTo(2);
19+
assertThat(result.next.next.val).isEqualTo(3);
20+
assertThat(result.next.next.next.val).isEqualTo(5);
21+
assertThat(result.next.next.next.next).isNull();
22+
}
23+
24+
@Test
25+
public void removeNthFromEnd2() {
26+
var head = new ListNode(1);
27+
28+
var result = RemoveNthNodeFromLinkedListEnd.removeNthFromEnd(head, 1);
29+
30+
assertThat(result).isNull();
31+
}
32+
33+
@Test
34+
public void removeNthFromEnd3() {
35+
var head = new ListNode(1, new ListNode(2));
36+
37+
var result = RemoveNthNodeFromLinkedListEnd.removeNthFromEnd(head, 1);
38+
39+
assertThat(result.val).isEqualTo(1);
40+
assertThat(result.next).isNull();
41+
}
42+
}

0 commit comments

Comments
 (0)