Solution
# Definition for singly-linked list.
from typing import Optional
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    # ์๊ฐ ๋ณต์ก๋: O(N)
    # Runtime: 754ms
    # Beats: 83.59%
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        # ์ฒ์์ head๊ฐ null์ธ ๊ฐ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ค์ผ ํ๋ ์ค ์์์์ง๋ง
        # ์คํ๋ ค ์๋๋ง ๋ฆ์ถ ๋ฟ ํ์๊ฐ ์์์
        # Optional๋ก ๊ฐ์ธ์ ๋ค์ด์ค๋ ๋น์ฐํ ํ์๊ฐ ์์ง
        array = []
        current = head
        # linkedList์ ๋ชจ๋  ๊ฐ์ array ๋ฐฐ์ด์ ์์ฐจ์ ์ผ๋ก ์ ์ฅ
        # current (head)๊ฐ null์ด ์๋ ๋ ๋ฐ๋ณต๋ฌธ ๋์์ฃผ์
        while current is not None:
            # ๋ฐฐ์ด์ ๊ณ์ ์ถ๊ฐํด์ฃผ๊ณ 
            array.append(current.val)
            # ๋
ธ๋ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์์ผ์ฃผ๊ธฐ
            current = current.next
        # [::-1] -> ๋ฐฐ์ด ๋ค์ง๊ธฐ
        # ๊ทธ๋ฅ ๋ฐฐ์ด๊ณผ ๊ทธ ๋ค์ง์ ๋ฐฐ์ด์ด ๋์ผํ๋ฉด true๋ฅผ ๋ฆฌํดํ๊ฒ๋
        return array == array[::-1]
 
        
      
      
      
      
  
     
    
      
    
  
Leave a comment