Skip to content

Commit 533328f

Browse files
committed
Add 'Delete Node In a Linked List', 'Remove Nth Node From End of List' and 'Rotate Image'
1 parent 4ea9a77 commit 533328f

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.github.juchanei.leetcodeJava
2+
3+
class DeleteNodeInALinkedList {
4+
class ListNode(var `val`: Int) {
5+
var next: ListNode? = null
6+
}
7+
8+
fun deleteNode(node: ListNode?) {
9+
val next = node?.next ?: return
10+
node.`val` = next.`val`
11+
node.next = next.next
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.github.juchanei.leetcodeJava
2+
3+
import org.assertj.core.api.BDDAssertions.then
4+
import org.junit.jupiter.api.Test
5+
6+
// https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/603/
7+
class RemoveNthNodeFromEndOfList {
8+
class ListNode(var `val`: Int, var next: ListNode? = null)
9+
10+
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
11+
if (head == null) return null
12+
13+
val dummy = ListNode(0, head)
14+
15+
findAndRemoveNode(dummy, n) + 1
16+
17+
return dummy.next
18+
}
19+
20+
private fun findAndRemoveNode(node: ListNode?, n: Int): Int {
21+
if (node == null) {
22+
return 0
23+
} else {
24+
val indexFromEnd = findAndRemoveNode(node.next, n) + 1
25+
26+
// 제거할 노드의 직전 노드에서 제거한다
27+
if (indexFromEnd - 1 == n) {
28+
removeNextNode(node)
29+
}
30+
31+
return indexFromEnd
32+
}
33+
}
34+
35+
private fun removeNextNode(node: ListNode) {
36+
node.next = node.next!!.next
37+
}
38+
39+
@Test
40+
fun example1() {
41+
val head = listOf(1, 2, 3, 4, 5).toListNode()
42+
val n = 2
43+
44+
val actual = removeNthFromEnd(head, n)
45+
46+
then(actual.toList()).isEqualTo(listOf(1, 2, 3, 5))
47+
}
48+
49+
@Test
50+
fun example2() {
51+
val head = listOf(1).toListNode()
52+
val n = 1
53+
54+
val actual = removeNthFromEnd(head, n)
55+
56+
then(actual.toList()).isEqualTo(emptyList<Int>())
57+
}
58+
59+
@Test
60+
fun example3() {
61+
val head = listOf(1, 2).toListNode()
62+
val n = 1
63+
64+
val actual = removeNthFromEnd(head, n)
65+
66+
then(actual.toList()).isEqualTo(listOf(1))
67+
}
68+
69+
@Test
70+
fun example4() {
71+
val head = listOf(1, 2).toListNode()
72+
val n = 2
73+
74+
val actual = removeNthFromEnd(head, n)
75+
76+
then(actual.toList()).isEqualTo(listOf(2))
77+
}
78+
79+
private fun List<Int>.toListNode(): ListNode? {
80+
if (isEmpty()) return null
81+
return ListNode(first(), drop(1).toListNode())
82+
}
83+
84+
private fun ListNode?.toList(): List<Int> {
85+
val result = mutableListOf<Int>()
86+
87+
var node: ListNode? = this
88+
while (true) {
89+
if (node == null) break
90+
result.add(node.`val`)
91+
node = node.next
92+
}
93+
94+
return result
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.github.juchanei.leetcodeKotlin
2+
3+
import org.junit.jupiter.api.Assertions.assertArrayEquals
4+
import org.junit.jupiter.api.Test
5+
6+
// https://leetcode.com/problems/rotate-image/
7+
class RotateImage {
8+
fun rotate(matrix: Array<IntArray>) {
9+
val n = matrix.size
10+
11+
for (a in 0 until n / 2) {
12+
for (b in a until n - a - 1) {
13+
val c = n - a - 1
14+
val d = n - b - 1
15+
16+
val one = matrix[a][b]
17+
val two = matrix[b][c]
18+
val three = matrix[c][d]
19+
val four = matrix[d][a]
20+
21+
matrix[a][b] = four
22+
matrix[b][c] = one
23+
matrix[c][d] = two
24+
matrix[d][a] = three
25+
}
26+
}
27+
}
28+
29+
@Test
30+
fun example1() {
31+
val expected = arrayOf(
32+
intArrayOf(7, 4, 1),
33+
intArrayOf(8, 5, 2),
34+
intArrayOf(9, 6, 3)
35+
)
36+
val matrix = arrayOf(
37+
intArrayOf(1, 2, 3),
38+
intArrayOf(4, 5, 6),
39+
intArrayOf(7, 8, 9)
40+
)
41+
rotate(matrix)
42+
43+
assertArrayEquals(expected, matrix)
44+
}
45+
46+
@Test
47+
fun example2() {
48+
val expected = arrayOf(
49+
intArrayOf(15, 13, 2, 5),
50+
intArrayOf(14, 3, 4, 1),
51+
intArrayOf(12, 6, 8, 9),
52+
intArrayOf(16, 7, 10, 11)
53+
)
54+
val matrix = arrayOf(
55+
intArrayOf(5, 1, 9, 11),
56+
intArrayOf(2, 4, 8, 10),
57+
intArrayOf(13, 3, 6, 7),
58+
intArrayOf(15, 14, 12, 16)
59+
)
60+
rotate(matrix)
61+
62+
assertArrayEquals(expected, matrix)
63+
}
64+
65+
@Test
66+
fun example3() {
67+
val expected = arrayOf(intArrayOf(1))
68+
val matrix = arrayOf(intArrayOf(1))
69+
rotate(matrix)
70+
71+
assertArrayEquals(expected, matrix)
72+
}
73+
74+
@Test
75+
fun test1() {
76+
val expected = arrayOf(
77+
intArrayOf(21, 15, 13, 2, 5),
78+
intArrayOf(22, 14, 3, 4, 1),
79+
intArrayOf(23, 12, 6, 8, 9),
80+
intArrayOf(24, 16, 7, 10, 11),
81+
intArrayOf(25, 10, 3, 2, 1),
82+
)
83+
val matrix = arrayOf(
84+
intArrayOf(5, 1, 9, 11, 1),
85+
intArrayOf(2, 4, 8, 10, 2),
86+
intArrayOf(13, 3, 6, 7, 3),
87+
intArrayOf(15, 14, 12, 16, 10),
88+
intArrayOf(21, 22, 23, 24, 25)
89+
)
90+
rotate(matrix)
91+
92+
assertArrayEquals(expected, matrix)
93+
}
94+
}

0 commit comments

Comments
 (0)