728x90

LeetCode-234

Palindrome Linked List : 주어진 head가 palindrome인지 아닌지
note : range : 1~10^5

MyAnswer

        :::python

# Definition for singly-linked list.# class ListNode:# def init(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: temp: List = [] if not head: return True node = head while node is not None: temp.append(node.val) node = node.next if len(temp)%2==0: return temp[:len(temp)//2] == temp[(len(temp)//2):][::-1] else: return temp[:len(temp)//2] == temp[(len(temp)//2)+1:][::-1]

Result : 855ms Memory: 46.6mb

Answer

        :::python

# Definition for singly-linked list.# class ListNode:# def init(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: q: Deque = collections.deque() if not head: return True node = head while node is not None: q.append(node.val) node = node.next while len(q) > 1: if q.popleft()!=q.pop(): return False return True

Result : 775ms Memory: 47.5mb

반응형
다했다