Skip to content

Instantly share code, notes, and snippets.

@yesidays
Created April 9, 2020 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yesidays/cdea856ea27cf2bbe15eefa45db87129 to your computer and use it in GitHub Desktop.
Save yesidays/cdea856ea27cf2bbe15eefa45db87129 to your computer and use it in GitHub Desktop.
Middle of the Linked List
#https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/
class Solution(object):
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
middle = head
current = head.next
i = 1
if(head.next is None):
return head
while (current.next is not None):
current = current.next
i += 1
if i % 2 == 0:
middle = middle.next
if i % 2 != 0:
middle = middle.next
return middle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment