kucheasysa

Algoverse_adesh_32

Jul 1st, 2024
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. # your task is to complete this function
  2.  
  3. '''
  4. class node:
  5.    def __init__(data):
  6.        self.data = data
  7.        self.next = None
  8. '''
  9. class Solution:
  10.     #  Should return data of middle node. If linked list is empty, then  -1
  11.     def findMid(self, head):
  12.         if head is None:
  13.             return -1
  14.            
  15.         slow = head
  16.         fast = head
  17.        
  18.         while fast is not None and fast.next is not None:
  19.             slow = slow.next
  20.             fast = fast.next.next
  21.            
  22.         return slow.data
  23.         # return the value stored in the middle node
  24.  
  25.  
  26.  
Advertisement
Add Comment
Please, Sign In to add comment