Middle of the Linked List (python) - Day 8/30 Leetcode April Challenge

preview_player
Показать описание
Larry solves and analyzes this Leetcode problem as both an interviewer and an interviewee. This is a live recording of a real engineer solving a problem live - no cuts or edits!

#leetcode #coding #programming
Рекомендации по теме
Комментарии
Автор

Hey everybody! For new problems, I will wait the 24 hours, I will only post older easily Google-able problems, thanks!

Algorithmist
Автор

I went with the latter method. Is my time and space complexities correct?
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
ptrA = head
ptrB = head
counter = 0
while ptrA.next is not None:
counter += 1
ptrA = ptrA.next

mid = counter // 2

if counter % 2:
mid += 1

for _ in range(mid):
ptrB = ptrB.next

return ptrB

# time complexity: O(N)
# space complexity: O(1)

minyoungan
Автор

Wasn't this question much easier than others....
BTW good solution

ankitatrivedi
join shbcf.ru