Late to the party, but didn't see one that did it the way I had in my head, so it is.
(defun gba-tmw ( lst / a b r )
(while (setq a (car lst))
(cond
((not r) (setq r (list a)))
((setq b (assoc (car a) r)) (setq r (subst (cons (car b) (append (cdr b) (cdr a))) b r)))
(t (setq r (cons a r)))
)
(setq lst (cdr lst))
)
(vl-sort r (function (lambda ( a b ) (< (car a ) (car b)))))
)
Here is one that will sort the nested lists also, incase that also desired.
(defun gba-tmw2 ( lst / a b r )
; (setq lst '((1 123 234) (2 234 345) (3 345 456) (2 456 567 133) (3 567 678 125)))
(while (setq a (car lst))
(cond
((not r) (setq r (list a)))
((setq b (assoc (car a) r)) (setq r (subst (cons (car b) (append (cdr b) (cdr a))) b r)))
(t (setq r (cons a r)))
)
(setq lst (cdr lst))
)
(vl-sort
(mapcar (function (lambda (a) (cons (car a) (vl-sort (cdr a) (function (lambda (x y) (< x y))))))) r)
(function (lambda ( a b ) (< (car a ) (car b))))
)
)