[TIL_Carrotww] 89 - 23/01/18

μœ ν˜•μ„Β·2023λ…„ 1μ›” 19일
0

TIL

λͺ©λ‘ 보기
104/138
post-thumbnail

πŸ“Carrotww의 μ½”λ”© 기둝μž₯

🧲 python algorithm interview

πŸ” 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
profile
Carrot_hyeong

0개의 λŒ“κΈ€