๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
234. Palindrome-Linked-list | 50.3% | Easy |
Palindrome Linked List - LeetCode
Can you solve this real interview question? Palindrome Linked List - Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Example 1: [https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg] Input: hea
leetcode.com
๋ฌธ์ ์์ฝ
๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๊ฐ ํฐ๋ฆฐ๋๋กฌ์ธ์ง ํ๋จํ์ฌ๋ผ
์กฐ๊ฑด 1) ๋จ์ผ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ํค๋๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค
์๊ฐํ๋ ํ์ด 1 : ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๊ฐ์ ๋ฐฐ์ด์ ๋ฃ์ด ํฐ๋ฆฐ๋๋กฌ ํ๋จ (789ms)
๋ฐฐ์ด์ ์ฝ์ ํ ๋ค, ์ฌ๋ผ์ด์ฑ ์ด์ฉํด ๊ฐ์ ๋น๊ต
์๊ฐํ๋ ํ์ด 2 : ๋ฐํฌ(Deque)์ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ์ต์ ํ (837ms)
pop ํจ์๋ฅผ ์ด์ฉํด์ ์์ชฝ ์์๋ฅผ ๋น๊ต
๋จ, deque ์๋ฃํ์ popleft ํจ์๋ฅผ ์ด์ฉํด์ pop(0)์ ์๊ฐ ๋ณต์ก๋์ธ O(n)์ O(1)๋ก ์ต์ ํ
๋ฐ๊ฒฌํ ์๋ก์ด ํ์ด 3 : Runner ๊ธฐ๋ฒ์ ํ์ฉ (637ms)
Runner ๊ธฐ๋ฒ์ '์ฐ๊ฒฐ๋ฆฌ์คํธ์ค๋ฌ์ด' ํ์ด๋ฅผ ์ํด ์ฌ์ฉ๋๋ฉฐ ๋น ๋ฅธ ๋ฌ๋์ ๋๋ฆฐ ๋ฌ๋ 2๊ฐ์ ํฌ์ธํฐ ์ด์ฉ
[ํ์ด์ฌ ์ฝ๋ฉํ ์คํธ ๊ฐ๋ ] 1-1. ์ฐ๊ฒฐ๋ฆฌ์คํธ - ๋ฌ๋
Runner ๊ธฐ๋ฒ Runner ๊ธฐ๋ฒ์ '์ฐ๊ฒฐ๋ฆฌ์คํธ์ค๋ฌ์ด' ํ์ด๋ฅผ ์ํด ์ฌ์ฉ๋๋ฉฐ ๋น ๋ฅธ ๋ฌ๋, ๋๋ฆฐ ๋ฌ๋ 2๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ๋ค. ๋น ๋ฅธ ๋ฌ๋(fast runner) : ๋๋ฆฐ ๋ฌ๋๋ณด๋ค 2๋ฐฐ ์์ ๋ค (๋ ์นธ์ฉ ์ด๋) ๋๋ฆฐ ๋ฌ๋(slow r
steady-eschoi.tistory.com
ํ์ด 1
๋ฐฐ์ด์ ๊ฐ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๊ฐ์ ๋ฃ์ ๋ค, ํฐ๋ฆฐ๋๋กฌ ํ๋จ
Step1. ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๊ฐ์ ๋ฐฐ์ด์ ์ ์ฅ
Step2. ํฐ๋ฆฐ๋๋กฌ ํ๋จ -> ์ฌ๋ผ์ด์ฑ ์ด์ฉ
๊ตฌํ
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
linked_list=[]
while head.next!=None:
linked_list.append(head.val)
head=head.next
linked_list.append(head.val)
if linked_list==linked_list[::-1]:
return True
return False
ํ์ด 2
๋ฐํฌ(deque) ์๋ฃํ์ ์ด์ฉ
Step1. ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ฐ์ ๋ฐํฌ์ ์ ์ฅ -> collections.deque() ์๋ฃํ ์ด์ฉ
Step2. ๋ฐํฌ์ ์์ชฝ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ํน์ฑ์ ์ด์ฉํด ํฐ๋ฆฐ๋๋กฌ ํ์ธ -> popleft(), pop() ํจ์ ์ด์ฉ
๊ตฌํ
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
deque=collections.deque()
while head.next!=None:
deque.append(head.val)
head=head.next
deque.append(head.val)
while len(deque)>1:
if deque.popleft()!=deque.pop():
return False
return True
ํ์ด 3 (637ms)
๋ฌ๋(Runner) ๊ธฐ๋ฒ ํ์ฉํด์ ์๋ก์ด ์ฐ๊ฒฐ๋ฆฌ์คํธ(๊ธฐ์กด์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ญ์์ผ๋ก ๋ง๋๋) ๋ง๋ค์ด ๋น๊ต
Step1. ๋ฌ๋ ๋ ๊ฐ ์์ฑํ์ฌ ๊ธฐ์กด์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ญ์์ผ๋ก ๋ง๋ค๊ธฐ
Step2. ๋๋ฆฐ ๋ฌ๋์ ์ญ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด ํฐ๋ฆฐ๋๋กฌ ํ์ธ
๊ตฌํ
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
reverse_head=None
slow=fast=head
#๋ฌ๋๋ฅผ ์ด์ฉํด ์ญ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ ์์ฑ
while fast and fast.next:
#๋น ๋ฅธ ๋ฌ๋๋ ๋์นธ์ฉ ์ด๋
fast=fast.next.next
reverse_head, reverse_head.next, slow=slow, reverse_head, slow.next
#๋๋ฆฐ ๋ฌ๋๋ ํ์นธ์ฉ ์ด๋
if fast: #์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๋
ธ๋์ ์๊ฐ ํ์์ด๋ฉด ๋ค์ ๋
ธ๋๋ก(๊ฐ์ด๋ฐ ๊ฐ์ ํฐ๋ฆฐ๋๋กฌ ๋น๊ต ๋์์ด ์๋)
slow=slow.next
while reverse_head!=None:
if reverse_head.val!=slow.val:
return False
reverse_head=reverse_head.next
slow=slow.next
return True
cf) ๋น ๋ฅธ ๋ฌ๋, ๋๋ฆฐ ๋ฌ๋, ์ญ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๋ฐฉํฅ๊ณผ ๋ฐฉ๋ฌธ๊ฒฝ๋ก ํ์ธ
์ฐ๊ฒฐ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ ํ์์ผ ๊ฒฝ์ฐ
์ฐ๊ฒฐ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ ์ง์์ผ ๊ฒฝ์ฐ
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 206. Reverse_Linked_List (Easy) 2023/5/5 (0) | 2023.05.05 |
---|---|
[LeetCode] 21. Merge_Two_Sorted_Lists (Easy) 2023/5/5 (0) | 2023.05.05 |
[LeetCode] 121. Best_Time_to_Buy_and_Sell_Stock (Easy) 2023/5/3 (0) | 2023.05.03 |
[LeetCode] 238. product_of_array_except_self (Medium) 2023/5/2 (0) | 2023.05.02 |
[LeetCode] 561. array_partition_i (Easy) 2023/5/2 (0) | 2023.05.02 |