티스토리 뷰
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
rev = None
slow = fast = head
# 런너를 이용해 역순 연결 리스트 구성
while fast and fast.next:
fast = fast.next.next
rev, rev.next, slow = slow, rev, slow.next
if fast:
slow = slow.next
# 팰린드롬 여부 확인
while rev and rev.val == slow.val:
slow, rev = slow.next, rev.next
return not rev
[출처]
https://github.com/onlybooks/algorithm-interview/blob/master/3-linear-data-structures/ch08/13-4.py
'PL > Python' 카테고리의 다른 글
[Python] function docstring, 파이썬 좋은 문서 작성하기 (0) | 2022.01.17 |
---|---|
[Python] Call by Object Reference (객체 참조에 의한 호출) (0) | 2022.01.17 |
[Python] is와 ==의 차이 (0) | 2022.01.05 |
유니코드(Unicode)와 UTF-8 (0) | 2021.12.29 |
[Python] 타입을 호환하는, Generics (0) | 2021.12.29 |