LeetCode - The World's Leading Online Programming Learning Platform
Given the head
of a singly linked list, return true
**if it is a palindrome or false otherwise.
from collections import deque
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
if not head:
return True
dq = deque()
while head is not None:
dq.append(head.val)
head = head.next
while len(dq) > 1:
if dq.popleft() != dq.pop():
return False
return True
python collection의 deque를 이용하여 앞에서부터 val 값을 넣고 앞 뒤로 빼면서 팰린드롬인지 확인하였다.
파이썬 알고리즘 인터뷰