leetcode 234
-
141. Linked List CycleProgramming/leetcode 2021. 2. 4. 23:40
주어진 linked list가 순환하는지 판별하는 문제이다. 이전에 234번에서 풀었던 Runner 기법을 이용하면 쉽게 풀린다. hongprogrammer.tistory.com/entry/234-Palindrome-Linked-List 234. Palindrome Linked List 주어진 linked list가 회문인지 판단하는 문제이다. 난이도는 쉽다. linked list를 직접 변경할 수 없으니, in-out이 쉬운 구조가 되어야한다. python에서는 list에서 특정 지점에서 pop도 지원한다. 1) list 이 hongprogrammer.tistory.com slow는 한칸씩 가면서, fast는 두칸씩 전진해서 나간다. 무한 반복해가면서 같은것이 나올때까지 돌린다. def hasCycle..
-
234. Palindrome Linked ListProgramming/leetcode 2021. 2. 4. 10:21
주어진 linked list가 회문인지 판단하는 문제이다. 난이도는 쉽다. linked list를 직접 변경할 수 없으니, in-out이 쉬운 구조가 되어야한다. python에서는 list에서 특정 지점에서 pop도 지원한다. 1) list 이용하는 방법 def isPalindrome(self, head: ListNode) -> bool: lst = [] if not head: return True while head: lst.append(head.val) head = head.next while len(lst)>1: if lst.pop(0) != lst.pop(): return False return True 위 방법은 pop할때마다 list를 재배치하기때문에 시간이 더 많이 들 수 밖에 없다. 그리고..