forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
234.PalindromeLinkedList.py
38 lines (33 loc) · 976 Bytes
/
234.PalindromeLinkedList.py
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
"""
Given a singly linked list, determine if it is a palindrome.
Example:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
"""
#Difficulty: Easy
#26 / 26 test cases passed.
#Runtime: 96 ms
#Memory Usage: 23.8 MB
#Runtime: 96 ms, faster than 31.45% of Python3 online submissions for Palindrome Linked List.
#Memory Usage: 23.8 MB, less than 94.98% of Python3 online submissions for Palindrome Linked List.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
nums = []
while head:
nums.append(head.val)
head = head.next
i = 0
j = len(nums) - 1
while i <= j:
if nums[i] != nums[j]:
return False
i += 1
j -= 1
return True