Skip to content

Commit 041262e

Browse files
committed
palindrome_lined_list
1 parent c45380a commit 041262e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
189189
#### [230. Kth Smallest Element in a BST](https://github.com/hitzzc/go-leetcode/tree/master/kth_smallest_element_in_a_BST)
190190
#### [231. Power of Two](https://github.com/hitzzc/go-leetcode/tree/master/power_of_two)
191191
#### [232. Implement Queue using Stacks](https://github.com/hitzzc/go-leetcode/tree/master/implement_queue_using_stacks)
192+
#### [234. Palindrome Linked List](https://github.com/hitzzc/go-leetcode/tree/master/palindrome_lined_list)
192193

193194

194195

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package palindrome_lined_list
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func isPalindrome(head *ListNode) bool {
9+
slow, fast := head, head
10+
for fast != nil && fast.Next != nil {
11+
slow = slow.Next
12+
fast = fast.Next.Next
13+
}
14+
head2 := reverse(slow)
15+
for ; head != nil && head2 != nil; head, head2 = head.Next, head2.Next {
16+
if head.Val != head2.Val {
17+
return false
18+
}
19+
}
20+
return true
21+
}
22+
23+
func reverse(head *ListNode) (newHead *ListNode) {
24+
var pre *ListNode
25+
cur := head
26+
for cur != nil {
27+
tmp := cur.Next
28+
cur.Next = pre
29+
pre = cur
30+
if tmp == nil {
31+
break
32+
}
33+
cur = tmp
34+
}
35+
return cur
36+
}

0 commit comments

Comments
 (0)