😤
LeetCode 234. Parindrome Linked List
Question
Given the head of a singly linked list, return true if it is a palindrome.
~~
線形リストの値が回文になっているかどうかを確認する問題
# Code
~~~kotlin
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun isPalindrome(head: ListNode?): Boolean {
val stack = mutableListOf<Int>()
var current = head
while (current != null) {
stack.add(current.`val`)
current = current?.next
}
val half = stack.size /2
for (idx in 0 until half) {
if (stack[idx] != stack[stack.lastIndex-idx]) return false
}
return true
}
}
一度普通のlistに詰めて両端から中心に向かって値を確認していく、
対数時間での実行は難しそう。
Profile
- Runtime: 569 ms
- Memory Usage: 96.5 MB
Discussion