-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path234.go
49 lines (46 loc) · 750 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
package easy_collection
/**
* 234. Palindrome Linked List
* <p>
* Given a singly linked list, determine if it is a palindrome.
* <p>
* Example 1:
* <p>
* Input: 1->2
* <p>
* Output: false
* <p>
* Example 2:
* <p>
* Input: 1->2->2->1
* <p>
* Output: true
* <p>
* Follow up:
* <p>
* Could you do it in O(n) time and O(1) space?
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func isPalindrome(head *ListNode) bool {
nums := make([]int, 0)
for head != nil {
nums = append(nums, head.Val)
head = head.Next
}
left := 0
right := len(nums) - 1
for left <= right {
if nums[left] != nums[right] {
return false
}
left++
right--
}
return true
}