Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- https://leetcode.com/problems/middle-of-the-linked-list/description/
- Given the head of a singly linked list, return the middle node of the linked list.
- If there are two middle nodes, return the second middle node.
- 2024.08.21 - I think what trips me up about the prompt is the requirement to return the second middle node. It sounds like it'll require some extra logic to handle, when in fact it's just the way things work by default if you use the fast-and-slow-pointer approach.
- """
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, val=0, next=None):
- # self.val = val
- # self.next = next
- class Solution:
- def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
- slow = fast = head
- while fast and fast.next:
- fast = fast.next.next
- slow = slow.next
- return slow
Advertisement
Add Comment
Please, Sign In to add comment