Skip to content

Commit ae26fa3

Browse files
author
konstantin
committed
Merge remote-tracking branch 'origin/master'
2 parents d3a06d9 + d70e99a commit ae26fa3

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/easy/234. Palindrome Linked List .kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package easy
22

3+
import LinkedListTopic
4+
import StackTopic
5+
import TwoPointersTopic
36
import utils.ListNode
47

58
/**
@@ -9,18 +12,18 @@ import utils.ListNode
912
* Given the head of a singly linked list, return true if it is a palindrome.
1013
*/
1114

12-
class Easy234 {
15+
class Easy234 : LinkedListTopic, TwoPointersTopic, StackTopic {
1316

1417
fun isPalindrome(head: ListNode?): Boolean {
1518
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
2124
}
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]) {
2427
return false
2528
}
2629
}

src/medium/61. Rotate List .kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)