File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
leetcode/palindrome-linked-list Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package util
Original file line number Diff line number Diff line change
1
+ package util
2
+
3
+ object ListNodeUtil {
4
+ fun generate (){
5
+
6
+ }
7
+ }
You can’t perform that action at this time.
0 commit comments