Created
October 18, 2020 14:49
-
-
Save inspirit941/de56e97b739d349d104c2f95058ab52d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
from collections import deque | |
class Solution: | |
def isPalindrome(self, head: ListNode) -> bool: | |
if not head: return True | |
# deque를 활용한 풀이 | |
# 링크드 리스트 데이터를 deque에 저장. | |
queue: Deque = deque() | |
node = head | |
while node: | |
queue.append(node.val) | |
node = node.next | |
while len(queue) > 1: | |
if queue.popleft() != queue.pop(): | |
return False | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment