By a sorting step on the segment endpoints, we can determine that each endpoint exists exactly twice. If this condition holds, then every endpoint forms a polygon vertex. If the condition does not hold, then $S$ cannot form a polygon. So, if this condition is true, we then connect the edges of $S$ according to their adjacencies and peform a traversal of these edges starting at some vertex $v$. If this traversal visits all the edges, then they form a polygon. If this traversal misses some edges, then the segments in $S$ form at least two polygons.