Created
April 9, 2020 00:07
-
-
Save yesidays/cdea856ea27cf2bbe15eefa45db87129 to your computer and use it in GitHub Desktop.
Middle of the Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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