-
[LeetCode] 234. Palindrome Linked List알고리즘 2022. 4. 24. 00:30
[LeetCode] 234. Palindrome Linked List
풀이_#1
공간복잡도 O(1), 시간복잡도 O(N) 의 풀이
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ slow, fast = head, head rev = None before = None while(fast and fast.next): fast = fast.next.next rev = slow slow = slow.next rev.next = before before = rev if (fast): type = "odd" else: type = "even" if (type == "odd"): slow = slow.next while (slow and rev): print(slow.val, rev.val) if (slow.val != rev.val): return False slow = slow.next rev = rev.next return True
'알고리즘' 카테고리의 다른 글
[LeetCode] 278. First Bad Version (0) 2022.04.24 [LeetCode] 704. Binary Search (0) 2022.04.24 [LeetCode] 1672. Richest Customer Wealth (0) 2022.04.24 [LeetCode] 1342. Number of Steps to Reduce a Number to Zero (0) 2022.04.23 [LeetCode] The K Weakest Rows in Matrix (0) 2022.04.23