I publish my original and fun solution on Postgre/PostGIS SQL for those who will encounter a situation where the lines have self-intersections,
So, the initial situation is shown in the figure, the initial table named line (LineString EPSG: 4326):

Create a new original function and launch a request to execute it:
CREATE OR REPLACE FUNCTION ST_SelfIntersectingLineRectifier(
geom GEOMETRY
)
RETURNS GEOMETRY AS
$BODY$
WITH
tbla AS (SELECT (ST_Dump(geom)).geom geom),
tblb AS (SELECT ST_MakeLine(pt1, pt2) geom FROM (SELECT ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) pt1,
ST_PointN(geom, generate_series(2, ST_NPoints(geom))) pt2 FROM tbla) AS geom),
tblc AS (SELECT DISTINCT ST_Union(a.geom) geom FROM tblb a, tblb b WHERE ST_Length(a.geom)<ST_Length(b.geom)),
tbld AS (SELECT DISTINCT (ST_Dump(ST_Difference(a.geom, b.geom))).geom geom FROM tblb a JOIN tblc b ON ST_Intersects(a.geom, b.geom)),
tble AS (SELECT ST_MakeLine(ST_LineSubstring(geom, 0.1, 0.9)) geom FROM tbld),
tblf AS (SELECT (a.geom) geom FROM tblb a JOIN tble b ON ST_Intersects(a.geom, b.geom)),
tblg AS (SELECT (a.geom) geom FROM tblf a WHERE NOT EXISTS (SELECT 1 FROM tbld b WHERE ST_Intersects(a.geom, ST_StartPoint(b.geom)))),
tblh AS (SELECT (a.geom) geom FROM tblb a JOIN tbld b ON ST_Touches(a.geom, b.geom) LIMIT 1),
tbli AS (SELECT ST_MakeLine(ST_EndPoint(a.geom), ST_EndPoint(b.geom)) geom FROM tblg a, tblh b)
SELECT ST_Union(geom) geom FROM (SELECT * FROM tblc UNION SELECT * FROM tbli) foo
$BODY$
LANGUAGE SQL
SELECT ST_SelfIntersectingLineRectifier(geom) geom FROM line
You can see the result in the picture below:

The script is called ST_SelfIntersectingLineRectifier
It's actually really hard to solve this problem analytically, but it's quite easy to solve it geometrically...
Remember, it's always important to define a nodal point or line that will help you find a solution...
"At first I run to your meeting, but then you force me to run from you..."
Translated with www.DeepL.com/Translator (free version)
ST_ShortestLinemay return a line which does not end on a vertex. In this case the linestrings cannot be rejoined by that line. In fact,, they can only be rejoined by a line segment between a pair of their endpoint. Is this what you want? – dr_jts Aug 22 '20 at 21:40ST_ShortestLinebecause it automatically calculates the endpoints for the new segment, right? In other words, the new segment to rejoin does not necessarily have to have the endpoints of the removed segment. I explain? – liap307 Aug 24 '20 at 13:38