Leetcode 908 Solution

This article provides solution to leetcode question 908 (middle-of-the-linked-list)

https://leetcode.com/problems/middle-of-the-linked-list

Solution

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummyhead = ListNode(0)
        dummyhead.next = head

        fast = dummyhead
        slow = dummyhead

        while fast:
            slow = slow.next
            fast = fast.next
            fast = fast.next if fast else None

        return slow