연결리스트3 [LEET CODE] 206. Reverse Linked List # Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Constraints: The number of nodes in the list is the range [0, 5000]. -5000 ListNode: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) # Solution 2 - 반복 구조로 뒤집기 # Definition for singly-linked.. 2021. 3. 22. [LEET CODE] 21. Merge Two Sorted Lists ( 연산자 우선순위, 변수 스왑) # Problem Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Constraints: The number of nodes in both lists is in the range [0, 50]. -100 ListNode: if (not l1) or (l2 and l1.val > l2.val): l1, l2 = l2, l1 # 작은 값을 계속 l1에 연결시킨다. if l1: l1.next = self.mergeTwoLists(l1.next, l2) return l1 # 연산자 우선순위 우선순위 연산자 설명.. 2021. 3. 22. [LEET CODE] 234. Palindrome Linked List ( 연결리스트, 런너 기법) # Problem Given the head of a singly linked list, return true if it is a palindrome. Constraints: The number of nodes in the list is in the range [1, 105]. 0 bool: q: List = [] if not head: return True node = head # 리스트 변환 while node is not None: q.append(node.val) node = node.next # 팰린드롬 판별 # 시작과 끝 하나씩 비교 while len(q) > 1: if q.pop(0) != q.pop(): return False return True # Solution 2 - 데크를 이용한 .. 2021. 3. 22. 이전 1 다음 반응형