Skip to content

Commit 852a5bb

Browse files
committed
1 parent 47c4d40 commit 852a5bb

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode.`palindrome-linked-list`
2+
3+
import ListNode
4+
5+
class Solution {
6+
fun isPalindrome(head: ListNode?): Boolean {
7+
if (head?.next == null) return true
8+
var slowP = head
9+
var fastP = head
10+
while (fastP != null) {
11+
slowP = slowP?.next
12+
fastP = fastP.next?.next
13+
}
14+
var lastP: ListNode? = null
15+
while (slowP != null) {
16+
val next = slowP.next
17+
slowP.next = lastP
18+
lastP = slowP
19+
slowP = next
20+
}
21+
var startP = head
22+
while (startP != null && lastP != null) {
23+
if (startP.`val` != lastP.`val`) return false
24+
startP = startP.next
25+
lastP = lastP.next
26+
}
27+
28+
return true
29+
}
30+
31+
// fun isPalindrome(head: ListNode?): Boolean {
32+
// if (head?.next == null) return true
33+
// val stack = Stack<Int>()
34+
// var tag = head
35+
// while (tag != null) {
36+
// stack.push(tag.`val`)
37+
// tag = tag.next
38+
// }
39+
// var start = head
40+
// while (start != null) {
41+
// if (start.`val` != stack.pop()) {
42+
// return false
43+
// }
44+
// start = start.next
45+
// }
46+
// return true
47+
// }
48+
}

src/main/kotlin/util/ListNode.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package util

src/main/kotlin/util/ListNodeUtil.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package util
2+
3+
object ListNodeUtil {
4+
fun generate(){
5+
6+
}
7+
}

0 commit comments

Comments
 (0)