-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.回文链表.go
63 lines (48 loc) · 814 Bytes
/
234.回文链表.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @lc app=leetcode.cn id=234 lang=golang
*
* [234] 回文链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func isPalindrome(head *ListNode) bool {
tmp := []int{}
cur := head
for cur != nil {
tmp = append(tmp, cur.Val)
cur = cur.Next
}
if len(tmp) == 1 {
return true
}
mid := len(tmp) / 2
for i := 0 ; i < mid ; i++ {
if tmp[i] != tmp[len(tmp) - i -1]{
return false
}
}
return true
}
func main() {
l1 := ListNode{Val: 1}
l2 := ListNode{Val: 2}
l3 := ListNode{Val: 2}
l4 := ListNode{Val: 1}
l1.Next = &l2
l2.Next = &l3
l3.Next = &l4
fmt.Printf("%t\n", isPalindrome(&l1))
}
// @lc code=end