I want use Tikz to create linked lists (the data structures, with info and next fields in each node). The lists are horizontal, i.e., each node to the right of the previous one.

But some nodes must be placed slightly above or below the main path. See an example:

linked list

I'm currently using something similar to (next)-- ++(0.7, 0) |- (node) to draw the arrow from one node to the next, but I have to manage the distances manually.

Perhaps I'm missing a "build in" way in Tikz to connect the nodes the way I need. Or must I take care of the calculations myself?

Just expecting to not have to recode something that already exists...

  • see page 70 -- ctan.imsc.res.in/graphics/pgf/base/doc/pgfmanual.pdf
    – js bibra
    Commented Mar 11, 2021 at 13:29
  • I see I can use a different approach than my current one. However the distances in the examples are hardcoded, so I guess I have to make some calculation myself. Great insights in the manual!
    – Jander
    Commented Mar 11, 2021 at 13:40
  • 1
    With the calc tikzlibrary you can use (next) -| ($(next)!.5!(node)$) |- (node) etc. Commented Mar 12, 2021 at 4:43

1 Answer 1


You can let TikZ do these things, regardless of whether you have positioned the nodes by hand or via a chain. You only need to dial some appropriate to path. The important ingredient in this example is \ifdim\n1>2pt, which changes the path depending on whether or not the levels of start and target are (roughly) the same. \n1 is the vertical distance, which has been measured with calc. Obviously you can do many similar tests to make things automatic.

\begin{tikzpicture}[2p/.style={rectangle split,rectangle split horizontal,
    rectangle split parts=2,rectangle split part fill={none,gray!50},draw},
    hconnect/.style={semithick,{Circle[width=2pt,length=2pt]}-Stealth,to path={
        let \p1=($(\tikztotarget)-(\tikztostart)$),\n1={abs(\y1)},
         (\tikztostart.two east-|\tikztostart.two south)[rounded corners=\n3] 
         -| ($(\tikztotarget)!0.5!(\tikztostart)$) |- (\tikztotarget) 
         (\tikztostart.two east-|\tikztostart.two south) -- (\tikztotarget) 
  \node[2p,right=2em of A](B){~20~~};   
  \node[2p,right=2em of B,yshift=1em](C){~30~~};    
  \draw[hconnect] (A) to (B);
  \draw[hconnect] (B) to (C);
 \begin{scope}[start chain=B going right,
    nodes={2p,on chain,join=by hconnect}]
  \path node{~10~~} node{~20~~} node[yshift=1em]{~30~~} 
    node[yshift=-1em]{~40~~} node{~50~~} node{~60~~};
 \path (B-6.two east-|B-6.two south) 
    node[circle,draw,inner sep=1.5pt,path picture={
    \draw(path picture bounding box.north east) -- 
        (path picture bounding box.south west);}]{};

enter image description here

  • 2
    +1 That's a very comprehensive and well executed answer. Congratulations.
    – SebGlav
    Commented Mar 11, 2021 at 17:36
  • That's great. I am not familiar to chains yet... a lot to learn. Thanks.
    – Jander
    Commented Mar 11, 2021 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .