The mass of the hanging part is M/3. Its center of mass is at a distance of (L/3)/2 = L/6 below the table edge. Work done = (Mass of hanging part) * g * (distance C.M. is lifted) = (M/3) * g * (L/6) = MgL/18.