The problem is an old one. The old answer is "use Object > Path > Simplify and set the tolerances as tight as you need, join the lines at first, if they are separate"
I suggest try Inkscape. It also has Path > Simplify which reduces nodes, but I have found it respects long straight line segments more than Illustrator's simplify. See an example:

The black shape has only joined line segments. The red shape is the same after a few Path > Simplify commands (=Ctrl+L)
The blue shape has got one more Ctrl+L. Top left corner is rounded. Now it's the time to press Undo and delete manually still a few nodes with the node tool.
You can keep a locked original below to see if the result is still acceptable. It's done in the next image:

In Illustrator using Simplify needs a little more manual edits. You must convert those anchors which should stay sharp back to corner points and move them back to their right places. In addition, the handles of the nearest smooth anchors needs a little adjustment. Fortunately those tweaks are easy if you have a locked copy of the original below and snap to points =ON.
Totally other measures are needed, if your line segments are not lines, but separate narrow closed paths. You get them easily for ex. vectorizing scanned drawings which have dashed lines. Then you can try to fill the interior with the Shape Builder or Live Paint Bucket.
A test example:

In the left there's a dashed stroke shape. The stroke width is 0,35mm It's outlined in the middle, there's no stroke to show it is separate closed loops.
Shape Builder cannot make a good shape from it. Gap detection doesn't help. The gaps will get filled randomly. The shape will be inaccurate. The gaps must be filled. One way to do it is to give thick enough stroke to the loop chain. It's done in the right.
The new stroke must be outlined again and the generated parts must be united in the pathfinder panel. That result can be treated ok with the shape builder. It is in use here:

The shape builder is set to give a green fill to show the result. In the next image the generated shape is without fill, only a green stroke. It's aligned with the loop chain:

We see that the new curve has offset about twice the width of the loop chain. Object > Path > Offset path > 0,7mm generates a new path which fits perfectly:

Now there's no problems with custom pattern brushes:
