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 = next
class 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 = next
class 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
'🐢 One step' 카테고리의 다른 글
[leetcode-206] Reverse Linked List (0) | 2023.03.15 |
---|---|
[leetcode-21] Merge Two Sorted Lists (0) | 2023.03.12 |
[leetcode-121] Best Time to Buy and Sell Stock (0) | 2023.03.09 |
[leetcode-238] Product of Array Except Self (0) | 2023.03.08 |
[leetcode-561] Array Partition (0) | 2023.03.07 |