π Leetcode 234 Palindrome Linked List Easy
λ§ν¬λ 리μ€νΈλ§ μκ³ μλ€λ©΄ κ°λ¨ν λ¬Έμ λ€
- λ΄ νμ΄
class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: temp = [] cur_head = head while 1: temp.append(cur_head.val) if cur_head.next: # λ€μ κ°μ΄ μ‘΄μ¬νλ©΄ κ³μ μ§ν cur_head = cur_head.next else: break if temp == temp[::-1]: return True return False
π Leetcode 21 Merge Two Sorted Lists Easy
λ°νκ°μ΄ 리μ€νΈμΈμ€ μκ³ μ°©κ°νκ³ νμμλ€ γ γ
μ± μμ μ¬κ·λ‘ κΉλνκ² νμμ΄μ μ¬λ €λ³Έλ€.# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if not list1 or (list2 and (list1.val > list2.val)): list1, list2 = list2, list1 if list1: list1.next = self.mergeTwoLists(list1.next, list2) return list1