876. Middle of the Linked List
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 80% Topics: Linked List, Two Pointers
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
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
"""
slow, fast = head, head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
return slow
Solution from kamyu104/LeetCode-Solutions · MIT