There's probably a really simple mathematical way to do this, but maths isn't my strong point so maybe the following brute-strength approach.
It's only a problem in the 360/0 boundary area. So let's define a band around that of maybe +-10, ie 350 to 10. Any readings outside this band are treated normally.
However if you get readings inside the band you switch to a mode that adds 360 to any values < 350, does the averaging, then subtracts 360 if the results is > 360.
If you get an average that falls outside the band you revert to the standard method.
(360+359+360+(1+360)+360 + (1+360))/6 = 360
(358+359+(5+360)+(4+360)+(3+360)+(1+360))/6 = 361.8, subtract 360 = 1.8
EDIT: all 360s should probably be 359s.
______
Rob