File tree Expand file tree Collapse file tree 2 files changed +43
-8
lines changed Expand file tree Collapse file tree 2 files changed +43
-8
lines changed Original file line number Diff line number Diff line change 1
1
package easy
2
2
3
+ import LinkedListTopic
4
+ import StackTopic
5
+ import TwoPointersTopic
3
6
import utils.ListNode
4
7
5
8
/* *
@@ -9,18 +12,18 @@ import utils.ListNode
9
12
* Given the head of a singly linked list, return true if it is a palindrome.
10
13
*/
11
14
12
- class Easy234 {
15
+ class Easy234 : LinkedListTopic , TwoPointersTopic , StackTopic {
13
16
14
17
fun isPalindrome (head : ListNode ? ): Boolean {
15
18
if (head == null ) return true
16
- val resultList = ArrayList <Int >()
17
- var currentNode = head
18
- while (currentNode != null ) {
19
- resultList .add(currentNode .`val `)
20
- currentNode = currentNode .next
19
+ val list = ArrayList <Int >()
20
+ var node = head
21
+ while (node != null ) {
22
+ list .add(node .`val `)
23
+ node = node .next
21
24
}
22
- for (i in 0 .. resultList .size / 2 ) {
23
- if (resultList [i] != resultList[resultList .size - 1 - i]) {
25
+ for (i in 0 .. list .size / 2 ) {
26
+ if (list [i] != list[list .size - 1 - i]) {
24
27
return false
25
28
}
26
29
}
Original file line number Diff line number Diff line change
1
+ package medium
2
+
3
+ import LinkedListTopic
4
+ import TwoPointersTopic
5
+ import utils.ListNode
6
+
7
+ /* *
8
+ * 61. Rotate List
9
+ * https://leetcode.com/problems/rotate-list/
10
+ *
11
+ Given the head of a linked list, rotate the list to the right by k places.
12
+ */
13
+
14
+ class Medium61 : LinkedListTopic , TwoPointersTopic {
15
+
16
+ fun rotateRight (head : ListNode ? , k : Int ): ListNode ? {
17
+ val list = ArrayList <ListNode >()
18
+ var node = head
19
+ while (node != null ) {
20
+ list.add(node)
21
+ node = node.next
22
+ }
23
+ val sentinel = ListNode (0 )
24
+ node = sentinel
25
+ for (i in list.indices) {
26
+ node?.next = list[(i - (k % list.size) + list.size) % list.size]
27
+ node = node?.next
28
+ }
29
+ node?.next = null
30
+ return sentinel.next
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments