imashutosh51

Middle of the Linked List

Apr 23rd, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution:
  7.     def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
  8.         slow=head
  9.         fast=head
  10.         while fast and fast.next:
  11.             slow=slow.next
  12.             fast=fast.next.next #fast.next is already checked in the while condition
  13.         return slow
  14.  
  15.        
Advertisement
Add Comment
Please, Sign In to add comment