-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome-linked-list.go
More file actions
56 lines (45 loc) · 925 Bytes
/
palindrome-linked-list.go
File metadata and controls
56 lines (45 loc) · 925 Bytes
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
package main
import (
"fmt"
. "github.com/dimaglushkov/solutions/ADS/list"
)
// source: https://leetcode.com/problems/palindrome-linked-list/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func isPalindrome(head *ListNode) bool {
l := 0
for p := head; p != nil; p = p.Next {
l++
}
if l == 0 || l == 1 {
return true
}
c := make([]int, 10)
for i, p := 0, head; p != nil; i, p = i+1, p.Next {
if i >= l/2 {
c[p.Val] ^= i
} else {
c[p.Val] ^= l - 1 - i
}
}
nonzeros := 0
for _, v := range c {
if v != 0 {
nonzeros += 1
}
}
return nonzeros == 0 || nonzeros == 1
}
func main() {
// Example 1
var head1 = NewList([]int{1, 2, 2, 1})
fmt.Println("Expected: true Output: ", isPalindrome(head1))
// Example 2
var head2 *ListNode = NewList([]int{1, 2})
fmt.Println("Expected: false Output: ", isPalindrome(head2))
}