-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path876.py
42 lines (31 loc) · 1.05 KB
/
876.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# [ LeetCode ] 876. Middle of the Linked List
class ListNode:
def __init__(self, val: int, next: "ListNode" | None) -> None:
self.val = val
self.next = next
def solution(head: ListNode) -> ListNode:
def get_size_of_linked_list(head: ListNode) -> int:
size: int = 0
while head:
size += 1
head = head.next
return size
def get_middle_of_linked_list(
head: ListNode, middle: int
) -> ListNode:
index: int = 0
while index < middle:
head = head.next
index += 1
return head
size: int = get_size_of_linked_list(head)
middle: int = size // 2
answer: ListNode = get_middle_of_linked_list(head, middle)
return answer
def another_solution(head: ListNode) -> ListNode:
slow_pointer: ListNode = head
fast_pointer: ListNode = head
while fast_pointer and fast_pointer.next:
slow_pointer = slow_pointer.next
fast_pointer = fast_pointer.next.next
return slow_pointer