Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EGON] Week9 Solutions #525

Merged
merged 6 commits into from
Oct 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: solve #225 with python
EgonD3V committed Oct 7, 2024
commit 1a2d3f1ff91d935bbcde4d17a85b42ab9fb54dd0
47 changes: 47 additions & 0 deletions linked-list-cycle/EGON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Optional
from unittest import TestCase, main


# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None


class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
return self.solve(head)

"""
Runtime: 37 ms (Beats 93.02%)
Time Complexity: O(n)
> head부터 next가 있는 동안 선형적으로 조회하므로 O(n)

Memory: 18.62 (Beats 98.22%)
Space Complexity: O(1)
> head를 제외하고 아무 변수도 사용하지 않았으므로 O(1)
"""
def solve(self, head: Optional[ListNode]) -> bool:
if not head:
return False

while head.next:
if head.next and head.next.val is None:
return True

head.val = None
head = head.next

return False


class _LeetCodeTestCases(TestCase):
def test_1(self):
head = None
output = False
self.assertEqual(Solution().hasCycle(head), output)


if __name__ == '__main__':
main()