[Leetcode] 234. Palindrome Linked List
[Leetcode] 234. Palindrome Linked List

[Leetcode] 234. Palindrome Linked List

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 08์›” 02์ผ
floatFirstTOC: right

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

์ฃผ์–ด์ง„ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ(ํšŒ๋ฌธ)์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฌธ์ œ๋‹ค. ์ฆ‰, ๋’ค์ง‘์–ด๋„ ๊ฐ™์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.
ย 
  1. head ๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด True๋กœ ํŒ์ •
  1. ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ deque์— ๋„ฃ์€ ํ›„, ์–‘์ชฝ ๋์„ ์„œ๋กœ ํŒ๋ณ„
    1. for i in range(length // 2): if queue[i] != queue[length - 1 - i]: return False
ย 

โš™๏ธย Python

from collections import deque class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def isPalindrome(self, head: ListNode) -> bool: if not head: # head๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด return True queue = deque() # head๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ queue์— ์‚ฝ์ž… while head is not None: queue.append(head.val) head = head.next # ์–‘์ชฝ ๋์œผ๋กœ ํŒ๋ณ„ length = len(queue) for i in range(length // 2): if queue[i] != queue[length - 1 - i]: return False return True
ย 

๐Ÿ“Œย ์†Œ๊ฐ

๋Œ“๊ธ€

guest