์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 234. Palindrome_Linked_list (Easy) 2023/5/4

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 5. 4. 22:36
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
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) ๋น ๋ฅธ ๋Ÿฌ๋„ˆ, ๋Š๋ฆฐ ๋Ÿฌ๋„ˆ, ์—ญ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ๊ณผ ๋ฐฉ๋ฌธ๊ฒฝ๋กœ ํ™•์ธ

 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๊ฐ€ ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ

 

ํ™€์ˆ˜) ๋น ๋ฅธ ๋Ÿฌ๋„ˆ, ๋Š๋ฆฐ๋Ÿฌ๋„ˆ, ์—ญ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ๊ณผ ๋ฐฉ๋ฌธ๊ฒฝ๋กœ

 

 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ

 

์ง์ˆ˜) ๋น ๋ฅธ ๋Ÿฌ๋„ˆ, ๋Š๋ฆฐ๋Ÿฌ๋„ˆ, ์—ญ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ๊ณผ ๋ฐฉ๋ฌธ๊ฒฝ๋กœ

728x90