Skip to content

Instantly share code, notes, and snippets.

@thekhairul
Created May 18, 2022 04:45
Show Gist options
  • Save thekhairul/b8a90da77a02c5760ee8b7b506d8600b to your computer and use it in GitHub Desktop.
Save thekhairul/b8a90da77a02c5760ee8b7b506d8600b to your computer and use it in GitHub Desktop.
Leetcode 876: Middle of a linked list - python
def middleNode(head):
#find total length
count = 1
cur = head
while cur.next:
count += 1
cur = cur.next
#find mid
mid = int(count/2 + 1)
#find mid node
cur = head
count = 1
while count <= mid:
if count == mid:
return cur
cur = cur.next
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment